PDA

View Full Version : I have never done programming in my life and need help with it! Arduino.



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.

Recaiden
2010-04-30, 09:30 PM
Do the delays serve any purpose?
And did you write these methods, or were they provided to you as a starting place?

Deathslayer7
2010-04-30, 09:32 PM
the delays dont necessarily provide any purpose besides the moment we plug it in the wheels start spinning. It is also in milliseconds.

Second this was provided for us.

bluewind95
2010-04-30, 09:43 PM
I don't know the programming language, though it seems similar to C...

THe high/low, though? That, I believe, is much like in electronics... the High would be 1 (that is... high enough current to trigger a True) while Low would mean 0 (that is, less current. False).

So for the backing up... you'd need to write the High/Low values that'd give the instruction to back up.

As far as I understand it... that seems to be it. The High/Low thing seems to hold true to my previous knowledge with the LED. Notice how it's Low when it's not on and High when it is.

Recaiden
2010-04-30, 09:45 PM
It seems simplest to me to restructure this to use a timer and a flag.

And it's C++based Wiring, I think.

Pyrian
2010-04-30, 09:51 PM
I don't know Arduino at all but it looks very C-ish and pretty simple.

Don't underestimate the significance of those delays. For example, changing the second-to-last one will (if I understand this correctly) modify the distance of the turn. Simply triple it to 1500 and instead of turning 90 degrees to the right, the robot will turn 270 degrees to the right - thereby heading left!

The change you're asking for is simple, in principle, as it doesn't require any logic, merely commands. The digitalWrite's appear to tell it how to move, while the delays immediately following them tell it how long. I'm not entirely sure how to make it turn the other way or reverse, but it's probably just low, low, high, low and low, high, low, high respectively.

So, replace the end-section with:

// Reverse!
digitalWrite(rightpin1, LOW);
digitalWrite(rightpin2, HIGH);
digitalWrite(leftpin1, LOW);
digitalWrite(leftpin2, HIGH);
delay(500);

// Dance right!
digitalWrite(rightpin1, HIGH);
digitalWrite(rightpin2, LOW);
digitalWrite(leftpin1, LOW);
digitalWrite(leftpin2, LOW);
delay(100);

// Dance left!
digitalWrite(rightpin1, LOW);
digitalWrite(rightpin2, LOW);
digitalWrite(leftpin1, HIGH);
digitalWrite(leftpin2, LOW);
delay(200);

// Turn around to the right
digitalWrite(rightpin1, HIGH);
digitalWrite(rightpin2, LOW);
digitalWrite(leftpin1, LOW);
digitalWrite(leftpin2, LOW);
delay(1000);

Deathslayer7
2010-04-30, 09:57 PM
it is similar to C++ as it is based off of it, except extremely simplified for non-programmers like me.

edit: i also thought the high/low was 1/0 but i wasnt sure.

Deathslayer7
2010-04-30, 10:10 PM
the reverse command doesnt seem to be working. the right wheel works but the left doesnt.

bluewind95
2010-04-30, 10:16 PM
This may be completely wrong if I don't recall the theory I know of this (granted, basic), properly... but I THINK that you can try switching the low and high for the left pins. That is... Left pin 1 should be 1, and left pin 2 should be 0. For the reverse thing. I think.

Deathslayer7
2010-04-30, 10:20 PM
that got me one wheel going forward and one back

Pyrian
2010-04-30, 11:18 PM
There's only 16 possible settings, you might have to just iterate them. :smallcool:

Deathslayer7
2010-04-30, 11:26 PM
There's only 16 possible settings, you might have to just iterate them. :smallcool:

i guess its only 16 and not say 64 or more. :smalltongue:

Deathslayer7
2010-04-30, 11:36 PM
ok did it and it makes no sense to me this is the table i got

0 0 0 0 LWF
0 0 0 1 X
0 0 1 0 LWF
0 0 1 1 X
0 1 0 0 LWF/RWB
0 1 0 1 RWB
0 1 1 0 LWF/RWB
0 1 1 1 RWB
1 0 0 0 LWF/RWF
1 0 0 1 RWF
1 0 1 0 LWF/RWF
1 0 1 1 RWF
1 1 0 0 LWF
1 1 0 1 X
1 1 1 0 LWF
1 1 1 1 X


anyone make sense of that? :smallconfused: There's not even a reverse on there.

Legend:
LWF= left wheel forward
RWB = right wheel back
etc.

Deathslayer7
2010-04-30, 11:47 PM
nevermind. switched a few wires around, tested it again and i got reverse on 0 1 1 0 as it should be. :smallsmile:

edit edit: but now i cant get foward on 1 0 0 1 :smallsigh:

FantomFang
2010-05-01, 12:00 AM
From what I see, I would agree with Pyrian, although from what I see I would seem to think that it would make it slightly off of 180 degrees, but that's irrelevant.

It does seem though that rightpin1 and leftpin1 control the forward motion of the wheels, so it would seem logical that rightpin2 and leftpin2 might control the reversed motion of the wheels.

Your truth table does seem to be confused in some way. But let's see...
Based on the truth table for your H bridge, it seems that the truth table is set up thusly:

rightpin1 rightpin2 leftpin2 leftpin1
1 0 0 1 Forward
0 1 1 0 Backwards
1 1 0 0 Brake (right wheel)
0 0 1 1 Brake (left wheel)

And it seems you've updated some wires. How does this affect your truth table now?

EDIT:

Ideally, I think we should get

0 0 0 0 No movement
0 0 0 1 LWF
0 0 1 0 LWB
0 0 1 1 Brake (Left Wheel)
0 1 0 0 RWB
0 1 0 1 RWB/LWF (accelerated right turn?)
0 1 1 0 Full reverse
0 1 1 1 RWB/Brake (Left Wheel)
1 0 0 0 RWF
1 0 0 1 Full forward
1 0 1 0 LWB/RWF (accelerated left turn?)
1 0 1 1 RWF/Brake (Left Wheel)
1 1 0 0 Brake (right wheel)
1 1 0 1 LWB/Brake (right wheel)
1 1 1 0 LWF/Brake (right wheel)
1 1 1 1 Full brake

Ofc, some of the situation will be don't cares, i would think, as 0111 wouldn't make much sense normally

EDIT EDIT:

Urgh, seems I'm wrong somewhere. Is there another way to make sense of the truth table then, that might give us the same theoretical outputs?

Deathslayer7
2010-05-01, 12:03 AM
Just as bad as the my first one. got reverse now but no forward.

Deathslayer7
2010-05-01, 12:09 AM
this is the new one i got by switching the wires.

0 0 0 0 RWB
0 0 0 1 X
0 0 1 0 RWB
0 0 1 1 X
0 1 0 0 Reverse
0 1 0 1 LWB
0 1 1 0 Reverse
0 1 1 1 LWB
1 0 0 0 LWF/RWB
1 0 0 1 LWF
1 0 1 0 LWF/RWB
1 0 1 1 LWF
1 1 0 0 RWB
1 1 0 1 X
1 1 1 0 RWB
1 1 1 1 X

FantomFang
2010-05-01, 12:12 AM
What pins are the truth values referencing specifically?
...
And I have to go now. Need to sleep for work :smallfrown:
Hopefully you can solve this before I finish tomorrow night, but otherwise I'll try and take another look. Good luck!

Deathslayer7
2010-05-01, 12:13 AM
if it happens before sunday afternoon i would be happy. and i need sleep myself. Getting late here. Thank you all for the help though. Appreciated. :smallsmile:

Rightpin1, rightpin2, leftpin1, leftpin2 in that order

Deathslayer7
2010-05-01, 12:58 PM
so anyone know whats wrong?

FantomFang
2010-05-01, 09:18 PM
Ok, back from work. Now, its research time...

Looking up an H bridge on shows the sort of info I was missing previously, having never worked with one previously, I should have done this first... so it seems that the left and right set of pins should never be closed at the same time, as that would short out the motor from the circuit under analysis. So I now see exactly how the old truth table works :smallbiggrin:

But I can't seem to find examples of using H-bridges to control turning operations. I would think that we would actually have two H-bridges implemented that would share the same inputs to the same ports... but then I don't know how we would achieve a turn...i guess that we need one wheel to spin backwards while the other goes forwards?

How do wire this up? Is there a single wire from each of the four inputs, or is there two from each, one for each motor? Because it seems that the values going into one of the wheels from the inputs should be opposite of the values going into the other wheels for the operation to work.

I wish I could play with this in person, it would be sooo much easier :smalltongue:

Deathslayer7
2010-05-03, 09:34 AM
thanks for the help but its ok. we are going back into the lab to get help :smalltongue: