Hey,
Im new with arduino and I looking for someone to wrote the code for my simple mission - Unfortunatley I failed and I hope somone will solve it for me.
The connnections:
pin 3 (Input) Hall sensor
5v and ground connected to the other 2 legs
I have tested with simple code and while the magnet is on - the hallstate is 0 (as expected).
The mission is using with Serial.begin / Serial.println
starting with printing the monitor the word "Right" - with loop- do not stop until the triger in the next line.
When the digitalwrite in the hall sensor is equal to 0 >> the monitor will write "Stop" for 10 seconds.
Now the main challenge is no matter what is the hall sensor state 0 or 1 the monitor will show "left" but not right in the former loop.
the monitor show the word "left" - with loop- do not stop until the triger in the next line.
When the digitalwrite in the hall sensor is equal to 0 >> the monitor will write "Stop" for 10 seconds.
I have tried this script but I cant stop the loop after the stop sentence. I looking for option to exit the loop for the former cases. It is the begginnig but doesnt answer that challenge.
const int hallpin = 2;
void setup() {
Serial.begin (9600);
pinMode (hallpin, INPUT) ;
}
void loop() {
Serial.println ("right");
if (digitalRead(hallpin) == 0) {
Serial.println ("stop");
while(digitalRead(hallpin) == 0) delay(100); //wait here till till the hallpin state changes
}
delay (2000); //"right" printed to serial monitor every 2s while hallpin == 1
}