Deathslayer7
2010-04-30, 08:40 PM
So yeah. Short story. Our professor gave us a week and a half to build a robot that works (done) and to program it to do something using Arduino.
If you don't know what Arduino is, you probably cant help me, but its a code program writer. We are using an H bridge to control two independant motors and this is the code that was given to use to do the following:
If the infared sensor sees a wall, the robot stops and turns to the left before continuing on.
Code (note the // are unimportant and the code does work, i tested it.):
int rightpin1 = 4;
int rightpin2 = 2;
int leftpin1 = 9;
int leftpin2 = 7;
int sensor = 0;
int val = 0;
int LED = 13;
void setup()
{
Serial.begin(9600); //serial. begin opens serial port and sets the
pinMode(rightpin1, OUTPUT); //baud for serial data transmission.
pinMode(rightpin2, OUTPUT);
pinMode(leftpin1, OUTPUT);
pinMode(leftpin2, OUTPUT);
pinMode(sensor, INPUT); // Here we defined the analog pin 1 (sensor pin)
pinMode(LED, OUTPUT); // as input since we’re inputting to the microcontroller
} // the values captured by the sensor
void loop()
{
val = analogRead(sensor); // analogRead would read a 10 bit analogue value that
delay(1000) ; //ranged from 0-1023;
while ( val < 550 )
{
// As along as the value captured by sensor is less than 550, the LED would stay //off, and the motor would go forward
digitalWrite(LED, LOW);
digitalWrite(rightpin1, HIGH);
digitalWrite(rightpin2, LOW);
digitalWrite(leftpin1, HIGH);
digitalWrite(leftpin2, LOW);
/*reading the value from the sensor inside the while loop is essential
because it would let us exit the loop if the value read is greater than 550
otherwise we will be stuck in an infinite loop. */
val = analogRead(sensor);
Serial.print("sensor_value:"); // will print the value read by sensor to the monitor
Serial.print(val, DEC);
delay(500);
}
// when the captured value of the sensor is bigger than 550 (means we’re //getting closer to a wall the robot will turn right, stop, and then go back to the //top of the Loop() function.
digitalWrite(rightpin1, HIGH);
digitalWrite(rightpin2, LOW);
digitalWrite(leftpin1, LOW);
digitalWrite(leftpin2, LOW);
delay(500);
digitalWrite(rightpin1, LOW);
digitalWrite(rightpin2, LOW);
digitalWrite(leftpin1, LOW);
digitalWrite(leftpin2, LOW);
digitalWrite(LED, HIGH);
delay(1000);
}
now what i want to do is this:
if the robot sees a wall, back up, turn around while doing a semi dance (and end up at 180 degrees to the wall) then keep going.
So I have never taken a programming class in my life and i do understand the code a bit, I'm not really sure how the input, Low/High works. So any help is appreciated.
The truth table associated with the H Bridge is this:
0011 Brake
0110 Reverse
1001 Forward
1100 Brake
The rest are either off or open circuit. In off, the wheels don't lock up it just rolls.
Thanks for any and all help.
If you don't know what Arduino is, you probably cant help me, but its a code program writer. We are using an H bridge to control two independant motors and this is the code that was given to use to do the following:
If the infared sensor sees a wall, the robot stops and turns to the left before continuing on.
Code (note the // are unimportant and the code does work, i tested it.):
int rightpin1 = 4;
int rightpin2 = 2;
int leftpin1 = 9;
int leftpin2 = 7;
int sensor = 0;
int val = 0;
int LED = 13;
void setup()
{
Serial.begin(9600); //serial. begin opens serial port and sets the
pinMode(rightpin1, OUTPUT); //baud for serial data transmission.
pinMode(rightpin2, OUTPUT);
pinMode(leftpin1, OUTPUT);
pinMode(leftpin2, OUTPUT);
pinMode(sensor, INPUT); // Here we defined the analog pin 1 (sensor pin)
pinMode(LED, OUTPUT); // as input since we’re inputting to the microcontroller
} // the values captured by the sensor
void loop()
{
val = analogRead(sensor); // analogRead would read a 10 bit analogue value that
delay(1000) ; //ranged from 0-1023;
while ( val < 550 )
{
// As along as the value captured by sensor is less than 550, the LED would stay //off, and the motor would go forward
digitalWrite(LED, LOW);
digitalWrite(rightpin1, HIGH);
digitalWrite(rightpin2, LOW);
digitalWrite(leftpin1, HIGH);
digitalWrite(leftpin2, LOW);
/*reading the value from the sensor inside the while loop is essential
because it would let us exit the loop if the value read is greater than 550
otherwise we will be stuck in an infinite loop. */
val = analogRead(sensor);
Serial.print("sensor_value:"); // will print the value read by sensor to the monitor
Serial.print(val, DEC);
delay(500);
}
// when the captured value of the sensor is bigger than 550 (means we’re //getting closer to a wall the robot will turn right, stop, and then go back to the //top of the Loop() function.
digitalWrite(rightpin1, HIGH);
digitalWrite(rightpin2, LOW);
digitalWrite(leftpin1, LOW);
digitalWrite(leftpin2, LOW);
delay(500);
digitalWrite(rightpin1, LOW);
digitalWrite(rightpin2, LOW);
digitalWrite(leftpin1, LOW);
digitalWrite(leftpin2, LOW);
digitalWrite(LED, HIGH);
delay(1000);
}
now what i want to do is this:
if the robot sees a wall, back up, turn around while doing a semi dance (and end up at 180 degrees to the wall) then keep going.
So I have never taken a programming class in my life and i do understand the code a bit, I'm not really sure how the input, Low/High works. So any help is appreciated.
The truth table associated with the H Bridge is this:
0011 Brake
0110 Reverse
1001 Forward
1100 Brake
The rest are either off or open circuit. In off, the wheels don't lock up it just rolls.
Thanks for any and all help.