Sensor Values

Hello everyone, I am currently trying to make a line following robot and with this code so far make a button start reading values from photoresists and change the threshold in my code currently.

Here is my code

const int leftMotorPin = 9; const int leftSensorPin = A0;
const int rightMotorPin = 8; const int rightSensorPin = A1;
const int buttonPin = 5; const int indicatorLedPin = 13;
int lightThreshold = 680;
int sensorValuel = 0; // variable to store the value coming from the sensor
int sensorValuer = 0; // variable to store the value coming from the sensor
int sensorValuem = 0; // variable to store the value coming from the sensor
int sensorPinl = A0; // select the input pin for LDR
int sensorPinr = A1; // select the input pin for LDR
void setup() {

pinMode(leftMotorPin, OUTPUT); pinMode(leftSensorPin, INPUT);
pinMode(rightMotorPin, OUTPUT); pinMode(rightSensorPin, INPUT);
Serial.begin(9600); //sets serial port for communication
}
void loop() {

int leftLightLevel = analogRead(leftSensorPin);
int rightLightLevel = analogRead(rightSensorPin);

if (leftLightLevel < lightThreshold)
digitalWrite(leftMotorPin, HIGH);
else
digitalWrite(leftMotorPin, LOW);
if (rightLightLevel < lightThreshold)
digitalWrite(rightMotorPin, HIGH);
else
digitalWrite(rightMotorPin, LOW);

sensorValuel = analogRead(sensorPinl); // read the value from the sensor
sensorValuer = analogRead(sensorPinr); // read the value from the sensor
sensorValuem = sensorValuel + sensorValuer; // add the values from both sensors
sensorValuem /= 2; // divides the added values but 2

Serial.println(sensorValuem); //prints the values coming from the sensor on the screen

delay(500);

}

What are your sensors? IR reflective types are popular for projects like this using a black line on a white background. People normally buy a module with an IR Transmitter bounced off a surface and the reflected IR measured. Some modules have adjustments for sensitivity. Should this be what you have move your sensor at correct height and angle over the white, using the serial monitor note the analog value. Move the sensor over black and again note the analog value. Those two values represent Black and White. The black runs the centerline of the robot and if either sensor begins to see black a correction is made. Going forward right sensor sees black you want to correct to the right. Give this a read and note how his code is applied.

Ron

My sensor is a photoresistor. Im trying to get my sensorvaluem to = the threshold and make the motor run is the threshold is greater than the threshold and stop is its less than

And what's your actual question?

What's your program doing or not doing that brought you to the forum....

I need help in trying to create a way to use a button to store one reading aka light then press the button again to store another reading aka dark then use those readings for the line robot move when it goes about light reading and stops when it goes below dark reading

boggieswag:
I need help in trying to create a way to use a button to store one reading aka light then press the button again to store another reading aka dark then use those readings for the line robot move when it goes about light reading and stops when it goes below dark reading

I gave you a link to read and suggested how to get the light and dark numbers you want. What part do you not understand?

Ron

Ron_Blain:
I gave you a link to read and suggested how to get the light and dark numbers you want. What part do you not understand?

Ron

I have to use sensors in my project and link you sent me using led's and motor drive. I can only use photoresistors and breadboard with base Arduino in my project.

boggieswag:
I need help in trying to create a way to use a button to store one reading aka light then press the button again to store another reading aka dark then use those readings for the line robot move when it goes about light reading and stops when it goes below dark reading

I would probably have a variable called say calibrationCounter or something, initialised to 0, and use state change detection to see that the "calibrateButton" has been newly pressed. If it is newly pressed, and calibrationCounter is 0, then read the sensor and save that value as lightThreshold, and increment calibrationCounter. Then if the button is seen as newly pressed again, and calibrationCounter is 1, read the sensor and save that as darkthreshold, set the counter back to 0.

boggieswag:
I have to use sensors in my project and link you sent me using led's and motor drive. I can only use photoresistors and breadboard with base Arduino in my project.

Your sensor values go where? You are reading over the line and not over the line right? So note the two values, over the line and not over the line. The link I gave you was about much more than LEDs and motor drivers, it was about sensors which is where it all begins. You run a sensor output into a uC. The sensor will read different values over a light or dark surface and those values are part of the decision making process for turning. This is where an understanding of how it works is important.

Ron