Hey everyone!
So I've been working on this arduino project where I take a photocell, get a value, convert from 0-1023 scale to 0-255 scale and operate one of two motors.
I wrote a code and I feel like my thinking is write but I cant seem to get the motors to run. Can anyone check to see whats wrong/missing? Thanks a bunch!
int LightSensorPin = 0;
int Read; // initializing a "Read" value
int val; // receive value that will drive the motor
int motorPin1 = 3;
int motorPin2 = 6;
void setup(void) {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
Serial.begin(9600); //Communication of sensor and computer
}
void loop(void) {
Read = analogRead(LightSensorPin);
Serial.print(" Reading = "); //print phrase
Serial.println(Read); //print analog reading of the light sensor
val = map(Read, 0, 1023, 0, 255); //map photocell values (0-1023) to analog values(0-255) for motor
if (val>150)
{
analogWrite(motorPin1, HIGH);
analogWrite(motorPin2, LOW);
}
if (val<101)
{
analogWrite(motorPin2, HIGH);
analogWrite(motorPin1, LOW);
}
delay(4000);
}