-1

I want to use a hall effect sensor with a timer (millis). The count should reset after 1 second and print out the given count.

const int Hallsensor=3;
int HallValue;
int LastHallValue;
int Hcount =0;
unsigned long timer = 0;
unsigned long start_time = 0;

void setup() {
  pinMode(Hallsensor, INPUT);
  Serial.begin(9600);
  counting = true;
  start_time = millis();
}

void loop() {
  LastHallValue = HallValue;
  timer = millis();
  if((timer - start_time) >= 1000) {
    HallValue = digitalRead(Hallsensor);
    if (HallValue == LOW and LastHallValue == HIGH) {
      Hcount++;
      Serial.println(Hcount);
    } else {
      if(counting == true) {
        Serial.print("Count is");
        Serial.println(Hcount);
        counting = false;
      }
    }
  }
}
  • Is your hall sensor one that outputs an analog signal proportional to the magnetic field strength, or does it output a logic-level signal? Please edit your question to include the part number of the hall sensor and/or the datasheet. – jose can u c Apr 3 '18 at 15:11
  • My hall sensor outputs a analog sensor proportional to the magnetic field strength. High and low works in our case (al though we need a physique amplifier). – Willis Houtman Apr 3 '18 at 20:03
0
const int Hallsensor=3;
int HallValue;
int LastHallValue;
int Hcount =0;

unsigned long timer = 0;
unsigned long lastChangeTime = 0;

void setup()
{

  pinMode(Hallsensor, INPUT);
  Serial.begin(9600);
   counting = true;
    start_time = millis();


}

void loop()
{

    LastHallValue = HallValue;
    HallValue = digitalRead(Hallsensor); 
    if (HallValue == LOW and LastHallValue == HIGH){
        Hcount++;
        lastChangeTime = millis();
        Serial.println(Hcount);   
    }
    else{
        if(counting == true){
            Serial.print("Count is");
            Serial.println(Hcount);
            counting = false;
            // Serial.println(LastHallValue);    
        }
    }

    if( (Hcount>0) && (millis() - lastChangeTime) >= 1000){
        Hcount = 0;// reset count when it hasn't increased in the last second.
    }

}
  • Just posting different code with no explanation is not a helpful answer. Indicate what you changed and provide at least a few words explanation of what problem that solves. – Chris Stratton Apr 3 '18 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.