I have a problem with stopping motor at same spot when magnet pass.. I tried to add interrupt and to make code as clean as possible but no joy so far. Any suggestions? sw will add later, just ignore..
byte outPin = 12; // the number of the output pin
byte inPin = 2; // the number of the input pin
byte hallPin = 3; // hall sensor
byte sw;
byte previous;
int state = LOW; // the current state of the output pin
int reading; // the current reading from the input pin
void setup()
{
pinMode(inPin, INPUT_PULLUP);
pinMode(outPin, OUTPUT);
pinMode(hallPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(hallPin), stop, FALLING); //hall read low when magnet pass
}
void loop()
{
sw=0;
reading = digitalRead(inPin); //read(LOW when press!)
if (reading == HIGH) previous=0;
if (previous == 1 && sw == 0) state=LOW;
else if (reading == HIGH) state=LOW;
else state=HIGH;
digitalWrite(outPin, state);
}
void stop() {
if (sw == 0 && reading == LOW) {
state=LOW;
previous=1;
}
}
state
's declaration tovolatile bool state
...