i got that part
I got that part down the logic behind how to do it but I have a problem with the programming i dont understand how to tell the arduino to compare the left sensor and right sensor and take the best choice.
current code
#define motorA 3 //Direction Pin for Left Motor
#define motorB 4 //Direction Pin for Left Motor
#define motorSpeedA 6 //Speed Pin for Left Motor
#define motorSpeedB 5 //Speed Pin for Right Motor
#define motorC 7 //Direction Pin for Right Motor
#define motorD 8 //Direction Pin for Right Motor
#define LDRPin A0
#define LDRPin1 A1
void setup() //here we are telling the Arduino that the motor pins should be outputs (not inputs)
{
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(motorC, OUTPUT);
pinMode(motorD, OUTPUT);
pinMode(motorSpeedA, OUTPUT);
pinMode(motorSpeedB, OUTPUT);
pinMode(LDRPin, INPUT);
pinMode(LDRPin1,INPUT);
(
Serial.begin(9600);
)
}
void loop()
{
analogWrite(motorSpeedA,255);
analogWrite(motorSpeedB,255);
}
void forward()
{
digitalWrite(motorC,LOW); //forward
digitalWrite(motorD,HIGH);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void backward()
{
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void leftSpin()
{
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void rightSpin()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void allStop()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,LOW);
}
Like ChuckCrunch said
LDRL = analogread(LDRPin)
LDRR = analogread(LDRPin1)
if (LDRL > LDRR) {
leftspin();
}
else if (LDRL < LDRR) {
rightspin();
}
else if (LDRL = LDRR) AND (LDRL > dark) { //dark being a constant set earlier to make robot phototropic
forward();
}
else {
backward();
Very helpful
Thank you birdmun and chuckcrunch is dark supposed to be a const int and the code you gave will the light follower be smooth as in will it follow a flashlight.
what i got so far
#define motorA 3 //Direction Pin for Left Motor
#define motorB 4 //Direction Pin for Left Motor
#define motorSpeedA 6 //Speed Pin for Left Motor
#define motorSpeedB 5 //Speed Pin for Right Motor
#define motorC 7 //Direction Pin for Right Motor
#define motorD 8 //Direction Pin for Right Motor
#define LDRPin A0
#define LDRPin1 A1
int dark;
void setup() //here we are telling the Arduino that the motor pins should be outputs (not inputs)
{
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(motorC, OUTPUT);
pinMode(motorD, OUTPUT);
pinMode(motorSpeedA, OUTPUT);
pinMode(motorSpeedB, OUTPUT);
pinMode(LDRPin, INPUT);
pinMode(LDRPin1,INPUT);
(
Serial.begin(9600);
)
}
void loop()
{
analogWrite(motorSpeedA,255);
analogWrite(motorSpeedB,255);
LDRL = analogread(LDRPin)
LDRR = analogread(LDRPin1)
if (LDRL > LDRR) {
leftspin();
}
else if (LDRL < LDRR) {
rightspin();
}
else if (LDRL = LDRR) AND (LDRL > dark) { //dark being a constant set earlier to make robot phototropic
forward();
}
else {
backward();
}
void forward()
{
digitalWrite(motorC,LOW); //forward
digitalWrite(motorD,HIGH);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void backward()
{
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void leftSpin()
{
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void rightSpin()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void allStop()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,LOW);
}
is this right I did not understand what the constant dark is.
what i got has error
#define motorA 3 //Direction Pin for Left Motor
#define motorB 4 //Direction Pin for Left Motor
#define motorSpeedA 6 //Speed Pin for Left Motor
#define motorSpeedB 5 //Speed Pin for Right Motor
#define motorC 7 //Direction Pin for Right Motor
#define motorD 8 //Direction Pin for Right Motor
#define LDRPin A0
#define LDRPin1 A1
int dark = 1000 // x being a mid point in your values
int bright = 550
void setup() //here we are telling the Arduino that the motor pins should be outputs (not inputs)
{
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(motorC, OUTPUT);
pinMode(motorD, OUTPUT);
pinMode(motorSpeedA, OUTPUT);
pinMode(motorSpeedB, OUTPUT);
pinMode(LDRPin, INPUT);
pinMode(LDRPin1,INPUT);
(
Serial.begin(9600);
)
}
void loop()
{
analogWrite(motorSpeedA,255);
analogWrite(motorSpeedB,255);
LDRL = analogread(LDRPin)
LDRR = analogread(LDRPin1)
if (LDRL < darkL) {
darkL = LDRL;
}
if (LDRL > brightL) {
brightL = LDRL;
}
if (LDRL > LDRR) {
leftspin();
}
else if (LDRL < LDRR) {
rightspin();
}
else if (LDRL = LDRR) AND (LDRL > dark) { //dark being a constant set earlier to make robot phototropic
forward();
}
else {
backward();
}
void forward()
{
digitalWrite(motorC,LOW); //forward
digitalWrite(motorD,HIGH);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void backward()
{
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void leftSpin()
{
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void rightSpin()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void allStop()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,LOW);
}
This compiles. I don’t know if it works.
const int motorA = 3; //Direction Pin for Left Motor
const int motorB = 4; //Direction Pin for Left Motor
const int motorSpeedA = 6; //Speed Pin for Left Motor
const int motorSpeedB = 5; //Speed Pin for Right Motor
const int motorC = 7; //Direction Pin for Right Motor
const int motorD = 8; //Direction Pin for Right Motor
#define LDRPin A0
#define LDRPin1 A1
const int dark = 1000; // x being a mid point in your values
const int bright = 550;
void setup() {//here we are telling the Arduino that the motor pins should be outputs (not inputs)
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(motorC, OUTPUT);
pinMode(motorD, OUTPUT);
pinMode(motorSpeedA, OUTPUT);
pinMode(motorSpeedB, OUTPUT);
pinMode(LDRPin, INPUT);
pinMode(LDRPin1,INPUT);
Serial.begin(9600);
}
void loop() {
int LDRL, LDRR;
analogWrite(motorSpeedA,255);
analogWrite(motorSpeedB,255);
LDRL = analogRead(LDRPin);
LDRR = analogRead(LDRPin1);
if (LDRL > LDRR) {
leftSpin();
}
else if (LDRL < LDRR) {
rightSpin();
}
else if ((LDRL = LDRR) && (LDRL > dark)) { //dark being a constant set earlier to make robot phototropic
forward();
}
else {
backward();
}
}
void forward() {
digitalWrite(motorC,LOW); //forward
digitalWrite(motorD,HIGH);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void backward() {
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void leftSpin() {
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void rightSpin() {
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void allStop() {
digitalWrite(motorC,LOW);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,LOW);
}
deadband
if ((LDRL > bright - 30) && (LDRL < bright + 30)) {
if ((LDRR > bright - 30) && ( LDRR < bright + 30)) {
forward();
}
}
const int motorA = 3;
const int motorA = 3; //Direction Pin for Left Motor
const int motorB = 4; //Direction Pin for Left Motor
const int motorSpeedA = 6; //Speed Pin for Left Motor
const int motorSpeedB = 5; //Speed Pin for Right Motor
const int motorC = 7; //Direction Pin for Right Motor
const int motorD = 8; //Direction Pin for Right Motor
#define LDRPin A0
#define LDRPin1 A1
const int dark = 30; // x being a mid point in your values
const int bright = 90;
void setup() {//here we are telling the Arduino that the motor pins should be outputs (not inputs)
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(motorC, OUTPUT);
pinMode(motorD, OUTPUT);
pinMode(motorSpeedA, OUTPUT);
pinMode(motorSpeedB, OUTPUT);
pinMode(LDRPin, INPUT);
pinMode(LDRPin1,INPUT);
Serial.begin(9600);
}
void loop()
{
int LDRL, LDRR;
analogWrite(motorSpeedA,255);
analogWrite(motorSpeedB,255);
LDRL = map(analogRead(0),200,32,0,100);
LDRR = map(analogRead(1),1005,597,0,100);
if (LDRL > LDRR) {
leftSpin();
}
else if (LDRL < LDRR) {
rightSpin();
}
else if ((LDRL > bright - 30) && (LDRL < bright + 30)) {
if ((LDRR > bright - 30) && ( LDRR < bright + 30)) {
forward();
}
}
else {
backward();
}
}
void forward() {
digitalWrite(motorC,LOW); //forward
digitalWrite(motorD,HIGH);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void backward() {
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void leftSpin() {
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void rightSpin() {
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void allStop() {
digitalWrite(motorC,LOW);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,LOW);
}
const int motorA = 3;
const int motorA = 3; //Direction Pin for Left Motor
const int motorB = 4; //Direction Pin for Left Motor
const int motorSpeedA = 6; //Speed Pin for Left Motor
const int motorSpeedB = 5; //Speed Pin for Right Motor
const int motorC = 7; //Direction Pin for Right Motor
const int motorD = 8; //Direction Pin for Right Motor
#define sensorpin A0
int range; // x being a mid point in your values
int danger = 90;
void setup() {//here we are telling the Arduino that the motor pins should be outputs (not inputs)
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(motorC, OUTPUT);
pinMode(motorD, OUTPUT);
pinMode(motorSpeedA, OUTPUT);
pinMode(motorSpeedB, OUTPUT);
pinMode(sensorpin,INPUT);
Serial.begin(9600);
danger=250;
}
void loop()
{
{
range=analogRead(sensorpin);
{
if (range>danger)
{
forward();
}
else if (range<danger) {
backward();
rightSpin();
}
else if (range=danger) {
allStop();
backward();
leftSpin();
}
}
else {
backward();
}
}
void forward();
{
digitalWrite(motorC,LOW); //forward
digitalWrite(motorD,HIGH);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void backward() {
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void leftSpin() {
digitalWrite(motorC,HIGH); //Left spin
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void rightSpin() {
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void allStop() {
digitalWrite(motorC,LOW);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,LOW);
}
The New And Shiny Code
Removed at the moment.
Just too much code in one page…
First of all, my apologies that I couldn’t take up this convo yesterday. Now, we’ve had a lot of code here already. I’d like to ask you to do the following things for me please before I can start posting code here-
1. Your function on motion (forward, reverse, stop etc.) look fine so we don’t need to edit those. So please (everyone!!) stop commenting those again and again. Just comment on serial and loop().
2. Tell me the pinout you have at this moment.
3. Have you solved the problem?
4. Tell me the problem you want to solve in words.
One thing I want to ask is that Can you measure the resistance of a LDR directly with an arduino? Can you measure the potential drop across a resistor without a pot? I mean directly from the arduino board? If you have to use a pot, then how will you connect it? Series? Parallel?
If I get the reply of everything and everything is as I understand it, then I have a code ready for you.
Reply Soon!!!
Just too much code in one page…
Crap double post…
for aaron
#define sensorpin
int motorA = 3; //Direction Pin for Left Motor
int motorB = 4; //Direction Pin for Left Motor
int motorSpeedA = 6; //Speed Pin for Left Motor
int motorSpeedB = 5; //Speed Pin for Right Motor
int motorC = 7; //Direction Pin for Right Motor
int motorD = 8; //Direction Pin for Right Motor
int range; // x being a mid point in your values
int danger = 250
void setup() //here we are telling the Arduino that the motor pins should be outputs (not inputs)
pinMode(motorA, OUTPUT)
pinMode(motorB, OUTPUT)
pinMode(motorC, OUTPUT);
pinMode(motorD, OUTPUT);
pinMode(motorSpeedA, OUTPUT);
pinMode(motorSpeedB, OUTPUT);
Serial.begin(9600);
void loop()
range=analogRead(sensorpin)
if (range < danger)
Forward();
else if (range >= danger)
Reverse();</p><p>
delay(500)
Stop()
Rightspin()
delay(600);
void Forward()
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
void Reverse()
digitalWrite(motorC,HIGH);
digitalWrite(motorD,LOW);
digitalWrite(motorA,HIGH)
digitalWrite(motorB,LOW);
void LeftSpin()
digitalWrite(motorC,HIGH);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
void Rightspin()
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
void Stop()
digitalWrite(motorC,LOW);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,LOW);
delay(10)
for robotmaster
#define IR 0
int motorA = 3; //Direction Pin for Left Motor
int motorB = 4; //Direction Pin for Left Motor
int motorSpeedA = 6; //Speed Pin for Left Motor
int motorSpeedB = 5; //Speed Pin for Right Motor
int motorC = 7; //Direction Pin for Right Motor
int motorD = 8; //Direction Pin for Right Motor
int range; //X to hold the range value
int danger = 250;
void setup() //here we are telling the Arduino that the motor pins should be outputs (not inputs)
{
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(motorC, OUTPUT);
pinMode(motorD, OUTPUT);
pinMode(motorSpeedA, OUTPUT);
pinMode(motorSpeedB, OUTPUT);
Serial.begin(9600);
}
void loop()
{
range = analogRead(IR);
if (range < danger)
{
Forward();
}
else if (range >= danger)
{
Stop();
Rightspin();
}
}
void Forward()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void Reverse()
{
digitalWrite(motorC,HIGH);
digitalWrite(motorD,LOW);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void LeftSpin()
{
digitalWrite(motorC,HIGH);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void Rightspin()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void Stop()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,LOW);
delay(10);
}
Now try out this baby.
light follower
int motorA = 3; //Direction Pin for Left Motor
int motorB = 4; //Direction Pin for Left Motor
int motorSpeedA = 6; //Speed Pin for Left Motor
int motorSpeedB = 5; //Speed Pin for Right Motor
int motorC = 7; //Direction Pin for Right Motor
int motorD = 8; //Direction Pin for Right Motor
// Variable definitions
int SensorRight; // This pin is used to read the value of the Right Sensor.
int SensorLeft; // This stores the value of the Right Sensor pin to use later on in the sketch
int SensorDifference; // This value is used to determine the difference between the Left and Right
// the setup() method runs once when the program is run. When the
// Arduino is reset, the setup() will be executed once again.
void setup()
{
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(motorC, OUTPUT);
pinMode(motorD, OUTPUT);
pinMode(motorSpeedA, OUTPUT);
pinMode(motorSpeedB, OUTPUT);
pinMode(LeftSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
pinMode(RightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
digitalWrite(motorSpeedA,HIGH);
digitalWrite(motorSpeedB,HIGH);
digitalWrite(A1, HIGH); // Enables an internal pullup resistor
digitalWrite(A2, HIGH); // Enables an internal pullup resistor
Serial.begin(9600); // Enables a serial connection through the Arduino to either USB or UART (pins 0&1). Note that the baud rate is set to 9600
Serial.println(" \nBeginning Light Seeking Behavior"); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop()
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
SensorLeft = 960 - analogRead(LeftSensor);
delay(1); // the delay allows the Analog to Digital converter IC to recover for the next reading.
SensorRight = 1020 - analogRead(RightSensor);
delay(1);
SensorDifference = abs(SensorLeft - SensorRight); // This calculates the difference between the two sensors and then saves it to an integer.
// This section of the sketch is used to print the values of the
// sensors through Serial to the computer. Useful for determining
// if the sensors are working and if the code is also functioning properly.
Serial.print(“Left Sensor = “); // Prints the text inside the quotes.
Serial.print(SensorLeft); // Prints the value of the Left Sensor.
Serial.print(”\t”); // Prints a tab (space).
Serial.print(“Right Sensor = “); // Prints the text inside the quotes.
Serial.print(SensorRight); // Prints the value of the Right Sensor.
Serial.print(”\t”); // Prints a tab (space).
// This section of the sketch is what actually interperets the data and then runs the motors accordingly.
if (SensorLeft > SensorRight && SensorDifference > 85)
{
LeftSpin();
}
if (SensorLeft < SensorRight && SensorDifference > 85)
{
RightSpin();
}
else if (SensorDifference < 150) {rints Forward when the robot would actually go forward.
}
Forward();
}
void Forward()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void Reverse()
{
digitalWrite(motorC,HIGH);
digitalWrite(motorD,LOW);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void LeftSpin()
{
digitalWrite(motorC,HIGH);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void Rightspin()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void Stop()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,LOW);
delay(10);
}