Wednesday, December 4, 2013

Coding

After a long time struggling with GitHub and the alien terminology (forking, committing, bashing, etc.) that goes with it, I finally found out how to put my code on there. First to tackle was the problem of implementing the LED lights to blink without using a 2 second delay that would disrupt all other functionality. My code focused on using iterations of the loop function as a counter that would go up to two seconds and, upon reaching that count, execute the light turning on/off. This is seen here:
//Blinking Light/ Solid Light
//Mike Igoe
//Code to make a one light blink at a rate of 2 seconds whilst another remains on.
//Pins
int redLED = 1;
int yellowLED = 2;
//Counter to act as the delay
int counter = 0;
void setup()
{
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
}
void loop()
{
//Red LED stays on.
digitalWrite(redLED, HIGH);
//increment counter to add time
//this assumes a loop is 1 millisecond - testing the program can change this
counter++;
//by the above assumption, this means two seconds passes by
if(counter>2000)
{
//turn on yellow
digitalWrite(yellowLED, HIGH);
if(counter > 4000)
{
//turn off yellow
digitalWrite(yellowLED, LOW);
//start over by making counter 0
counter=0;
}
}
}
}
view raw Boat_Light hosted with ❤ by GitHub


The issue with this was that you needed to know just how long it takes for a loop function to execute – I estimated on the low side and figured I could raise the counter limit until it equated to 2 seconds. Bobby figured out a way that would both avoid multiple tests as well as the loops:
//Two Light, Single Blink Boat Code
//Robert Fisher
//Design Lab 1 Fall 2013
// This code produces a blinking light with out having a delay that would interupt
// other functions and processes. Simple and easy.
int ledPin = 9; // Red LED pin
int ledYel = 10; // Yello LED pin
int i = 0; // Variable to contain time stamp 1; begin at 0
int i2 = 0; // Variable to contain time stamp 2; begin at 0
int j = 0; // Variable to count change; begin at 0
void setup()
{
pinMode(ledPin, OUTPUT);// Red LED is OUTPUT
pinMode(ledYel, OUTPUT);// Yellow LED is OUTPUt
Serial.begin(9600); // Begin Serial Com
}
void loop()
{
digitalWrite(ledPin, HIGH); // Red LED remains on constantly
i = (millis())/2000; // Set time stamp 1
delay(50); // delay between Timestamps to be tested
i2 = (millis())/2000; // Set time stamp 2
if (i != i2) // if time stamps do not match:
{
j++; // increment j by one
}
if (j == 0) // while j is 0, true:
{
digitalWrite(ledYel, LOW); // Yellow LED is off
Serial.println("Light is off, and current run time is:"); // Serial print light is off
Serial.println((millis())/1000);
Serial.println("sec");
}
if (j == 1)
{
digitalWrite(ledYel, HIGH); // Yellow LED is on
Serial.println("Light is on, and current run time is:");// Serial print light is on
Serial.println((millis())/1000);
Serial.println("sec");
}
if (j > 1) // if j is larger than 1:
{
j = 0; // set j back to 0
}
}
view raw gistfile1.txt hosted with ❤ by GitHub


I also worked on coding the servo motor and ac motor so that their movements would respond to a potentiometer. This was a test that prepare us for integrating the motors with the iPhone interface – since the adjustment of the potentiometer is similar to that of the app. Each is shown below:
//Mike Igoe
//Using a potentiometer to control AC motor speed
//Pins
int sensorPin = 0;
int motorPin = 9;
void setup()
{
pinMode(motorPin, OUTPUT);
}
void loop()
{
int sensorValue = analogRead(sensorPin);
//to convert the potentiometer to the ac motor, i make a relation to
//the max motor speed (255) and max potentiometer reading (1023)
//so, poten/4.01176470588 = max ----since we deal with integers I'll simplify it to 4
int speed = sensorValue/4;
analogWrite(motorPin, speed);
}
//
view raw AC_motor hosted with ❤ by GitHub
//Mike Igoe
//controlling servo motor with potentiometer
#include <Servo.h>
Servo serv1;
int sensorPin = 0;
void setup()
{
//attach servo to digital pin 9
servo.attach(9);
}
void loop()
{
int sensorValue = analogRead(sensorPin);
//As with the AC motor, the sensorvalue must be converted into terms relating to the servo
//Max angle = 180, max potentiometer reading = 1023
//so, poten / 5.68333 = angle --- by reducing the number to 5, we are in fact reducing
//the maximum possible angle that can be rotated; for this demonstration this should be negligble.
int position = sensorValue/5;
servo.write(position);
}
/* Note, with the iPhone app grid setup, this wouldn't work since it only moves in one direction.
To fix this, I'm wondering if negative values can go into "servo.write" to reverse the servo direction.
If so, I can change the formula converting potentiometer to servo where a reading 511.5 is zero degrees and readings of 0 and 1023 are -180 and 180 degrees, respectfully.
*/
view raw Servo hosted with ❤ by GitHub


Bobby then put the blinking light code together with the servo code so it was all in one. All that would be needed now is to add the motor in this:
#include <Servo.h>
//Two Light, Single Blink Boat Code
//Robert Fisher
//Design Lab 1 Fall 2013
// This code produces a blinking light with out having a delay that would interupt
// other functions and processes. Simple and easy.
int ledPin = 8; // Red LED pin
int ledYel = 7; // Yello LED pin
int i = 0; // Variable to contain time stamp 1; begin at 0
int i2 = 0; // Variable to contain time stamp 2; begin at 0
int j = 0; // Variable to count change; begin at 0
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(ledPin, OUTPUT);// Red LED is OUTPUT
pinMode(ledYel, OUTPUT);// Yellow LED is OUTPUt
Serial.begin(9600); // Begin Serial Com
}
void loop()
{
digitalWrite(ledPin, HIGH); // Red LED remains on constantly
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
i = (millis())/2000; // Set time stamp 1
delay(50); // delay between Timestamps to be tested
i2 = (millis())/2000; // Set time stamp 2
if (i != i2) // if time stamps do not match:
{
j++; // increment j by one
}
if (j == 0) // while j is 0, true:
{
digitalWrite(ledYel, LOW); // Yellow LED is off
//Serial.println("Light is off, and current run time is:"); // Serial print light is off
//Serial.println((millis())/1000);
//Serial.println("sec");
if ((analogRead(potpin))/5 < 90)
{
Serial.println("Left Turn Initiated");
}
}
if (j == 1)
{
digitalWrite(ledYel, HIGH); // Yellow LED is on
//Serial.println("Light is on, and current run time is:");// Serial print light is on
//Serial.println((millis())/1000);
//Serial.println("sec");
if ((analogRead(potpin))/5 > 90)
{
Serial.println("Right Turn Initiated");
}
}
if (j > 1) // if j is larger than 1:
{
j = 0; // set j back to 0
}
}


Finally Bobby introduced a unique idea to use the light sensor for the LED in place of the potentiometer – that way, the screen would adjust itself according to the amount of light around it:
//Robert Fisher
//Design Lab 1 Boat Project
//LCD Screen with Light Sensor
//You may have to replace this library call. To do that
//go to sketch>>import library>>Liquid Crystal. The
//"#include seen below will be placed at the top of your sketch.
//Erase the one below and the sketch should be okay to be loaded.
#include <LiquidCrystal.h>
const int contrastSensor = 0; // Light Sensor pin
const int contrastPin = 6; // PWM Output to LCD screen [j28]
//see SIK Guide>>circuit 15, page 78 in the component section
int contrast, high = 0, low = 1023; // set int contrast to read
// multiple levels
LiquidCrystal lcd(12,11,5,4,3,2); // Pins for LCD
void setup()
{
lcd.begin(16, 2); // Sets LCD rows and columns
lcd.clear();
lcd.print("hello, world!"); // This will eventually be
//turning status.
Serial.begin(9600);// so we can see it on th ecomputer as well.
}
void loop()
{
contrast = analogRead(contrastSensor); // read sensor
if (contrast < low)// Compound reading
{
low = contrast;
}
if (contrast > high)
{
high = contrast;
}
contrast = map(contrast, low+100, high-100, 0, 250);
//Maps values of sensor and also makes the ranges smaller.
//I did this because the sensor is not like the potentiometer.
contrast = constrain(contrast, 100, 150);
//Constrains the values between 100 and 150
analogWrite(contrastPin, contrast); // sets LCD contrast.
Serial.println(contrast); // just to print the output for testing
lcd.setCursor(0,1);//set cursor position, column 0 and bottom row
lcd.print(millis()/1000); // print second count
lcd.setCursor(13,1); // sets cursor position to 13th column
// and the bottom row.
lcd.print(contrast); // print contrast reading at set position.
}
view raw LCD_lightsensor hosted with ❤ by GitHub



With this code we still have the trouble of connecting it to the iPhone app, however. More work will need to be done before any of this can be tested and debugged, but so far it looks good.


- Mike

-       

No comments:

Post a Comment