Hi guys, can anyone tell me whether there’s anything wrong with my program below ? I’m trying to make the motor turn left/right when the potentiometer reading is <512, and right/left vice versa when reading is >512. Help is greatly appreciated.
const int analogInPin = A0;
int d = 9;
int e = 10;
int f = 12;
please use the # button when posting code, looks so much better
you allways write HIGH to F, think the first one should be low???
a variation
const int analogInPin = A0;
int d = 9;
int e = 10;
int f = 12;
int sensorValue;
int state = LOW;
void setup()
{
Serial.begin(115200);
Serial.println("Start"):
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
}
void loop()
{
// do an average reading to prevent flux
for (int i = 0; i< 4; i++) sensorValue += analogRead(0);
sensorValue /= 4;
// prevent noise area
if(sensorValue < 500 && state == HIGH) state = LOW;
if(sensorValue > 525 && state == LOW) state = HIGH;
// execute
if (state == LOW)
{
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
}
else
{
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
}
Serial.print(sensorValue);
Serial.print("\n");
}
Hi there ! I tried the program you pasted, the Serial Monitor shows some weird corrupted signals and the motor spins at a certain value and stop like what I exactly did previously.