Pages: [1]   Go Down
Author Topic: LDR Sensor with Adafruit Motor Shield  (Read 564 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've got a problem with adding ldr sensor to arduino code i did try many codes but they didn't work and i don't know why. Someone out there please help me it's my school project.
Here is the code;
Quote
#include <AFMotor.h>

int ldrPin = A0;
int lightValue = 0;
AF_DCMotor motor(4); // define motor on channel 4 with 1KHz default PWM
AF_DCMotor left_motor(1);  // define motor on channel 1 with 64KHz PWM

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  motor.setSpeed(200);     // set the speed to 200/255
  left_motor.setSpeed(200);
  lightValue = analogRead(ldrPin);
  pinMode(lightValue,INPUT);
  delay(1000);
}

void loop() {
  Serial.begin(9600);
  lightValue = analogRead(ldrPin);
  if(lightValue < 5)
  {
  motor.run(FORWARD);
  left_motor.run(FORWARD);  // turn it on going forward
  }
  else
  {
  motor.run(BACKWARD);
  left_motor.run(BACKWARD);  // turn it on going forwar
  }
}

Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 105
Posts: 4687
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does it compile?- if not, what compile errors did you get?

If it did compile, it helps to be more specific than saying  "i did try many codes but they didn't work".... what exactly doesn't work and in what way.

I would advise putting 3 more serial prints in... one just after you read the ldrpin to show what the reading is, and one in each "leg" of the if / else to say it got to the "if" or to the "else" then you'll know how well your logic is working.

Lose the serial begin inside loop().

Also it's better to put the code in the code tags (the #), not quote....
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This sets the pin for LDR to input, but you use the pinMode() with lightValue, not with the ldrPin.
I think there is no need for pinMode() if you use analogRead(), but I'm not sure.
Code:
// not good
lightValue = analogRead(ldrPin);
  pinMode(lightValue,INPUT);

Code:
// better
pinMode(ldrPin,INPUT);
lightValue = analogRead(ldrPin);

Is the LDR connected to A0 and GND ? You also need a pull-up resistor (perhaps 22k) from A0 to 5V.
« Last Edit: March 23, 2013, 09:55:13 am by Erdin » Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 105
Posts: 4687
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well spotted, Erdin...
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, i compile it and it doesn't give any error and i used ldr sensor which have 3 pins +5V,GND and Analog Input. I haven't got a time to test the code that you post, but i try and reply it again.
Logged

Pages: [1]   Go Up
Jump to: