We're in a High School electronics technology class and my group is attempting to program a robot that follows a black line.
We blindly mangled this code together from 2 tutorial programs with the idea being when a photoresistor sense black, the motors run. The code compiles correctly, but doesn't achieve the desired result when tested.
Our teacher has no idea how to program ardunio, and we don't have enough class time left to learn how. We are pretty desperate at this point, and any information at all would prove benefical.
Thanks,
Clarkes Class
/*
*/
const int AO1 = 13; //control pin 1 on the motor driver for the right motor
const int AO2 = 12; //control pin 2 on the motor driver for the right motor
const int PWMA = 11;
const int PWMB = 10; //speed control pin on the motor driver for the left motor
const int BO2 = 9; //control pin 2 on the motor driver for the left motor
const int BO1 = 8; //control pin 1 on the motor driver for the left motor
int sensorPin = A0; // select the input pin for ldr
int sensorPin2 = A1;
int sensorValue = 0; // variable to store the value coming from the sensor
int sensorValue2 = 0;
void setup() {
pinMode(2, OUTPUT); //pin connected to the relay
pinMode(AO1, OUTPUT);
pinMode(AO2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(BO1, OUTPUT);
pinMode(BO2, OUTPUT);
pinMode(PWMB, OUTPUT);
Serial.begin(9600); //sets serial port for communication
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
sensorValue = analogRead(sensorPin2);
Serial.println(sensorValue2);
if(sensorValue < 100) //setting a threshold value
{
digitalWrite(AO1,HIGH); //turn relay ON
digitalWrite(AO2,HIGH);
}
else //turn relay OFF
{
digitalWrite(AO1,LOW);
digitalWrite(AO2,LOW);
}
if(sensorValue2 < 100) //setting a threshold value
{
digitalWrite(BO1,HIGH); //turn relay ON
digitalWrite(BO2,HIGH);
}
else //turn relay OFF
{
digitalWrite(BO1,LOW);
digitalWrite(BO2,LOW);
}
}
Rather than just "doesn't achieve the desired result" it would be more helpful if you gave some detail of what the code does and what exactly you want it to do that's different. Details of what components like motors/motor driver(s)/sensors etc you are using with a circuit diagram showing how everything is connected would also be helpful.
The first thing I notice from a quick scan of the code is that you are reading from both sensors into the same variable so sensorValue2 is always 0. Didn't you notice that from your Serial.printlns?
int sensorPin = A0; // select the input pin for ldr
int sensorPin2 = A1;
int sensorValue = 0; // variable to store the value coming from the sensor
int sensorValue2 = 0;
Do you count uh-huh, 2, 3, ...? Or do you count 1, 2, 3, ...?
When numbering seemingly related variables, number ALL of them. That will save you untold grief later on.
Like right there. You crap in sensorValue, and then print sensorValue2.
You also do NOT define what you are printing, so you are guessing when it comes time to compare the output to the program. Don't guess. Make the output crystal clear.
digitalWrite(AO1,HIGH); //turn relay ON
When defining AO1, you claimed that is was connected to a motor driver. I'm willing to be that the motor driver does NOT use relays.
We are using SparkFun Inventor's Kit Version 4.0, so a SparkFun RedBoard and the Motor Driver they gave us in that kit.
Currently our project is just an arduino, breadboard, 2 photoresistors, 2 lose motors + wires and resistors. We are attempting to make it so the motors run when the photoresistors sense black.
When we get this working we are going to turn it into a car, which we already have made the frame for.
Thanks for that tip! While everyone in the group has basic programming expierences, nobody is familiar with C++ or arudino, and we are kind of in a time crunch.
Below I have attached photos of what we have, and a handdrawn circuit diagram, hope these work.