Simple little photovore

So, I've done the basics, I've sodered all the parts to the board ( motors, hbrige, headers) and I dropped in aurduino code and can get the wheels of my little photovore to turn. So I can pop in the batters and get it to run a frenetic circle around the table.

The only problem is that I don't understand code in the slightest. I haven't been able to run alot of the tests, the blink, etc.

I've attached one photodiode to the board with a 10 om resistor to the A0 port. I think I may need two in order to drop in someone else's code. There are no buttons on this....I hope to learn more as I go, but it was a group project that ended up being my project only and It's due in a week.

If someone can point me to aurduino photovore code I'd much appreciate it. I found the thread "my first photovore" but I'm so confused

What is a photovore?
From the context it sounds like some sort of robot.

I've attached one photodiode to the board with a 10 om resistor to the A0 port.

How?
And 10 ohm ( if that is what it is, is far too low a value to use )

Ok, it's crap to be dropped by your group, been there done that. But you'll get this sorted easily in a week, especially if you've done the building work.

So sit back and think.... forget the code for a minute.... write down how YOU would react if you were to follow light and you were sitting in a wheel chair. Assume someone is pointing a torch (or is that a flashlight?- don't know where you live 8) ) at you and you needed to follow it. What would you do if the light was a bit to the left, or a lot to the left, or straight ahead, or far right.

What would you do.

Think first, code last....

This is a bit worrying though:

I haven't been able to run a lot of the tests, the blink, etc.

.... you should get the basics under your belt. What problems have you had there: let's see if we can help there, first.

And yep @GM, a photovore is a light-following-robot.

It is a robot....but a VERY simple one.

did I ruin the photo diaode if I tried it with a 10 om? I just googled "aurduino photodiode" What should be on it?

I know one went to Ground and the other went to VCC (using an mini).

Hi Jimbo,

I HAVE been able to run the code, I haven't been able to duplicate it on my own. So far dropping in most of the really simple codes work really well. Blink and then the one I have in here now for the motor.

I have this code in and it causes it to go in circiles unless bumped and then it stops.

const int
PWM_A   = 3,
DIR_A   = 12,
BRAKE_A = 9,
SNS_A   = A0;


void setup() {
  // Configure the A output
  pinMode(BRAKE_A, OUTPUT);  // Brake pin on channel A
  pinMode(DIR_A, OUTPUT);    // Direction pin on channel A

  // Open Serial communication
  Serial.begin(9600);
  Serial.println("Motor shield DC motor Test:\n");
}

void loop() {

// Set the outputs to run the motor forward

  digitalWrite(BRAKE_A, LOW);  // setting brake LOW disable motor brake
  digitalWrite(DIR_A, HIGH);   // setting direction to HIGH the motor will spin forward

  analogWrite(PWM_A, 255);     // Set the speed of the motor, 255 is the maximum value

  delay(5000);                 // hold the motor at full speed for 5 seconds
  Serial.print("current consumption at full speed: ");
  Serial.println(analogRead(SNS_A));

// Brake the motor

  Serial.println("Start braking\n");
  // raising the brake pin the motor will stop faster than the stop by inertia
  digitalWrite(BRAKE_A, HIGH);  // raise the brake
  delay(5000);

// Set the outputs to run the motor backward

  Serial.println("Backward");
  digitalWrite(BRAKE_A, LOW);  // setting againg the brake LOW to disable motor brake
  digitalWrite(DIR_A, LOW);    // now change the direction to backward setting LOW the DIR_A pin

  analogWrite(PWM_A, 255);     // Set the speed of the motor

  delay(5000);
  Serial.print("current consumption backward: ");
  Serial.println(analogRead(SNS_A));

  // now stop the motor by inertia, the motor will stop slower than with the brake function
  analogWrite(PWM_A, 0);       // turn off power to the motor

  Serial.print("current brake: ");
  Serial.println(analogRead(A0));
  Serial.println("End of the motor shield test with DC motors. Thank you!");


  while(1);
}

Before we continue, can you go back to that previous post and Modify so instead of using Quote (the speech bubble), use Code (the #), muuuuuch easier to read.

JimboZA:
Before we continue, can you go back to that previous post and Modify so instead of using Quote (the speech bubble), use Code (the #), muuuuuch easier to read.

Thanks, just took me a second to find. The # totally makes sense, lol!

Cool now, a few things come to mind about that code:

  • First its purpose is to measure and display a motor's current draw, you don't need that
  • Second it drives only one motor, hence the circle
  • Three, it doesn't anywhere in there do anything to do with reading the photodiode and reacting thereto, does it....

So, sit back and think about how this should work, in words, not code.

BTW, you should show a circuit of what you have so far. What h-bridge is it?

JimboZA:
Cool now, a few things come to mind about that code:

  • First its purpose is to measure and display a motor's current draw, you don't need that
  • Second it drives only one motor, hence the circle
  • Three, it doesn't anywhere in there do anything to do with reading the photodiode and reacting thereto, does it....

So, sit back and think about how this should work, in words, not code.

BTW, you should show a circuit of what you have so far. What h-bridge is it?

Here's the datasheet for the H-bridge. http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Components/General%20IC/NJM2670.pdf

Are there tools to show you what I've done so far? My wires are horrible and not pretty.

There are tools, but you're probably not in the mood right now to learn how to use Eagle or Express which are available as free downloads. So why not draw it out on paper, take a photo with your phone, and attach that to a post by following Additional Options to attach a file.

I've never seen that h-bridge before myself, but application (2) on p5 shows exactly how it hooks up.

For each motor you give it 2 "INs" from the processor, INA1&2 for one motor "A" say, as well as the "ENABLE" to allow that motor to run. What is usually done, I guess, is to use the INs for motor direction and the EN for speed. A digitalWrite high / low on the inputs will give one direction, and reversing to low/high will spin the other way. If you use analogWrite to PWM the EN pin, you'll get speed control.

So, you need to read up on your pin control in the Reference here, top right.

edit.... Oh and btw, what that pic shows as Vmm is the power to the motor, not to be confused with the power to drive the h-bridge chip itself, which is shown as Vcc on that pic.

JimboZA:
There are tools, but you're probably not in the mood right now to learn how to use Eagle or Express which are available as free downloads. So why not draw it out on paper, take a photo with your phone, and attach that to a post by following Additional Options to attach a file.

That diagram is difficult to follow.... not least because if that's your h-bridge bottom right, it's not got all its pins. There ought to be 22 according to the datasheet, 2x11. So best to add them all and number them.

Is that other thing with 2 rows of horizontal pins the Arduino?

It's night time for me so I'm signing off now, but here's what I think you should try:

  • Think it through and write down how you would be your robot yourself.
  • Read up on the digital and analog outputs and try to write a sketch of your own that sets one motor's pins hi/low and low/high with the enable pin doing the speed control. That code you have probably be a good place to start.
  • Google how a photodiode is set up in a divider, here's one I found: How do I connect a photodiode? - Electrical Engineering Stack Exchange. Your drawing shows 3 wires though....
  • Have a look at http://arduino.cc/en/Tutorial/AnalogReadSerial which explains how to read an analog sensor. It's a pot, but thats effectively what your diode in a divider is- it puts out a voltage and thats what you need to read
  • Then, read this: http://arduino.cc/en/Tutorial/AnalogInOutSerial which uses the pot's reading to PWM an LED: that's basically what you want to do... your divider will give an input and you use that top PWM a motor.

JimboZA:
That diagram is difficult to follow.... not least because if that's your h-bridge bottom right, it's not got all its pins. There ought to be 22 according to the datasheet, 2x11. So best to add them all and number them.

Is that other thing with 2 rows of horizontal pins the Arduino?

It's night time for me so I'm signing off now, but here's what I think you should try:

  • Think it through and write down how you would be your robot yourself.
  • Read up on the digital and analog outputs and try to write a sketch of your own that sets one motor's pins hi/low and low/high with the enable pin doing the speed control. That code you have probably be a good place to start.
  • Google how a photodiode is set up in a divider, here's one I found: How do I connect a photodiode? - Electrical Engineering Stack Exchange. Your drawing shows 3 wires though....
  • Have a look at http://arduino.cc/en/Tutorial/AnalogReadSerial which explains how to read an analog sensor. It's a pot, but thats effectively what your diode in a divider is- it puts out a voltage and thats what you need to read
  • Then, read this: http://arduino.cc/en/Tutorial/AnalogInOutSerial which uses the pot's reading to PWM an LED: that's basically what you want to do... your divider will give an input and you use that top PWM a motor.

I'll be uploading a video soon. It's working.

  1. My drawing of a photodiode is innacurate--I did wire it correctly, just didn't draw it correctly.
  2. I put 100 OMS on my photodioeds... ports A0 and A1
  3. The surface mount H-Bridge is the bigger one. Mine has 8 on each side, not 11
/*
A1-Left side photovore activate at 1000 "EyeL"
A0- Right side photovore activate at 1000 "EyeR"



Plug both Reds into the middle, black on side of motor

9-Right wheel backwards
6-Left wheel forwards "motorLF"
5-Left wheel backwards
3-Right wheel forwards "motorRF"
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
const int motorRF = 3;
const int motorLF = 6;

const int en = 10;

const int EyeL = A1;
const int EyeR = A0;

int sensorValueEyeL = 0;     
int sensorValueEyeR = 0;     

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(motorRF, OUTPUT);
  pinMode(motorLF, OUTPUT);
  digitalWrite (en, HIGH);  
}

// the loop routine runs over and over again forever:
void loop() {

  sensorValueEyeR = analogRead(EyeR);
  sensorValueEyeL = analogRead(EyeL);
  if (sensorValueEyeL >980){
    analogWrite(motorRF, 25);  // turn the motor             
  } else {
    analogWrite(motorRF, 255);
  }
  if (sensorValueEyeR >980){
    analogWrite(motorLF, 5);   // turn the motor
  }else{
    analogWrite(motorLF, 255);
  }
  delay(10);      

  // wait for a second
}

I do think one of the motors is bad though because it just kinda flakes out when it feels like it.