Code Challenge - Simple hall sensor mission- unsolved right now

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:

  1. pin 3 (Input) Hall sensor
  2. 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

  1. starting with printing the monitor the word "Right" - with loop- do not stop until the triger in the next line.
  2. When the digitalwrite in the hall sensor is equal to 0 >> the monitor will write "Stop" for 10 seconds.
  3. 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.
  4. the monitor show the word "left" - with loop- do not stop until the triger in the next line.
  5. When the digitalwrite in the hall sensor is equal to 0 >> the monitor will write "Stop" for 10 seconds.

End of mission

Thanks for the solvers in advance!!!

sounds like a school project.. what have you tried so far?

post your attempts... everyone love a trier.

If you are planning to take this further in the future, begin thinking about a ‘state-machine’ as the core of your logic.

The first thing to understand is the input difference between off, on and changing between the two. (State change detection)

hopefully code(s) on this link might give you some ideas on how to do just that! :wink:

How to use Hall Effect Sensor with Arduino

hope that helps...

Thanks guys!

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");
delay (2000);

}
}

crude but something like this you mean?

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
}

hope that helps...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.