The screenshot is what we have built in a virtual space. The goal is to have the motors spin up and down as the light sensor detects more or less light. I'm having difficulty coding to have it scale.
analogWrite(left_motor, left_reading);
Is what I currently have but I want it so that when the left_reading is 0 the motor is 0 and as the left_reading goes to 1.00 I want the motor to be 255, but I have no idea how I would code that. Below I will paste the entirety of my code and to my knowledge it is correct and scaled correctly for the sensors.
// left sensor
int left_sens = A0 ;
// right sensor
int right_sens = A2 ;
// left motor
int left_motor = 6 ;
// right motor
int right_motor = 5 ;
// variable for storing sensor reading
float left_reading = 0;
float right_reading = 0;
void setup()
{
// setup motor pin as outputs
pinMode(left_motor, OUTPUT);
pinMode(right_motor, OUTPUT);
// setup sensor pins as input
pinMode(left_sens, INPUT) ;
pinMode(right_sens, INPUT) ;
// serial port for sensor readings
Serial.begin(115200);
}
void loop()
{
// read left sensor
left_reading= analogRead(left_sens);
// read right sensor
right_reading = analogRead(right_sens);
// Scale sensor readings
left_reading = (left_reading-1017)/(1017-344)*-1;
right_reading = (right_reading-1017)/(1017-344)*-1;
// print out sensor readings
Serial.print(left_reading);
Serial.print(',');
Serial.println(right_reading);
// adjust motor speeds
analogWrite(left_motor, left_reading);
delay(200);
}