Controlling timer with analogRead instead of digitalchange

Hi there,

i tried to reproduce a "plug & play" timer for my espresso machine based on a github code (GitHub - alexrus/marax_timer). i want to optimize the timer-trigger, the rest works perfectly fine.
the setup uses a reed switch glued onto the machine's vibration pump as trigger for the timer. i want to change it depending on the lever switch of the machine. i already found the correct pins using 0 - 5 V. i managed to read the information which looks like this:

10:55:24.030 -> 1
10:55:24.076 -> 29
10:55:24.076 -> 0
10:55:24.076 -> 30
10:55:24.076 -> 1024
10:55:24.076 -> 1024
10:55:24.114 -> 1024
10:55:24.114 -> 1024
10:55:25.748 -> 1024
10:55:25.748 -> 17
10:55:25.787 -> 15
10:55:25.787 -> 8

so my idea was to trigger the timer with the analogRead == 1024 and stop is with a value <1024

the original code looks like this:

#define D7 (13)

#define PUMP_PIN D7
...
void detectChanges() {
  digitalWrite(LED_BUILTIN, digitalRead(PUMP_PIN));
  if (!timerStarted && !digitalRead(PUMP_PIN)) {
    timerStartMillis = millis();
    timerStarted = true;
    displayOn = true;
    Serial.println("Start pump");
  }
  if (timerStarted && digitalRead(PUMP_PIN)) {
    if (timerStopMillis == 0) {
      timerStopMillis = millis();
    }
    if (millis() - timerStopMillis > 500) {
      timerStarted = false;
      timerStopMillis = 0;
      timerDisplayOffMillis = millis();
      display.invertDisplay(false);
      Serial.println("Stop pump");
    }
  } else {
    timerStopMillis = 0;

but i have no clue how to manage the problem: i started to interpret the analogRead as a digital so the original code should have worked, but the output HIGH LOW didnt worked.
i watched/read a bunch of tutorials so far, but cant transfer it to my problem. i dont want to mess up the rest of the code.

so: do u have any suggestions/tutorials for me?

beste regards
bjoern

if you want to use an analog input pin as digital input you can simple do :

pinMode(A0, INPUT); //set analog input A0 as digital input for example
.
.
.
digitalRead(A0); //<---- will then return you HIGH or LOW value

if you want to use at ACTUAL analogRead() to be interpreted as a digital signal, then you shouldn't use the MAX value but rather a hysterisis ie for example

if(analogRead(A0) > 680){ //lower limit for a 'HIGH'
     Serial.println("HIGH INPUT");
}
else if(analogRead(A0) < 340){ //upper limit for a 'LOW'
     Serial.println("LOW INPUT");
}

the value in the above snippet are total arbitrary and can/should be adjusted based on your current setup.

hope that helps...

That's the wrong way around :wink: Probably you meant > 680 (HIGH) and < 340 (LOW).

On AVR based Arduinos like Uno and Mega, analogRead() will never return 1024. So if you're using one of thise, I'm reasonably sure that the code that produced that was wrong.

absolutely!

Good catch! :+1:

reply #2 snippet updated accordingly

Setting a time stamp to zero, is a highly suspicious activity. I stopped reading after that.

thanks for your help so far, after connecting and using another resistance i monitored other values, so i tried this:

int analogPin = A0; 

void setup() {
  Serial.begin(9600);        
}

void loop() {
  if(analogRead(A0) > 0){ 
     Serial.println("Up");
}
else if(analogRead(A0) < 1){ 
     Serial.println("Down");
}
}

works fine, the "up" and "down" describes the position of the lever

after this i tried to change the timercode itself

...
int analogPin = A0;
...
void detectChanges() {
  digitalWrite(LED_BUILTIN, analogRead(A0));
  if (timerStarted && analogRead(A0) > 0) {
    timerStartMillis = millis();
    timerStarted = true;
    displayOn = true;
    Serial.println("Start pump");
  }
  if (timerStarted && analogRead(A0) < 1) {
    if (timerStopMillis == 0) {
      timerStopMillis = millis();
    }
    if (millis() - timerStopMillis > 500) {
      timerStarted = false;
      timerStopMillis = 0;
      timerDisplayOffMillis = millis();
      display.invertDisplay(false);
      Serial.println("Stop pump");
    }
  } else {
    timerStopMillis = 0;
...

the timer/Serial.print doenst work, only the LED lights up if the lever is in "up" position, anything i missed?

@sterretje im using a ESP8266
@anon57585045 i dont get it: "highly suspicious activity"?

Yes, using millis() in any standard way. Please explain why you are setting a time stamp to zero. That would represent boot time.

i want to use and display it as a timer, so i thought setting it to 0 is the way to do it :o

without seeing your full code, I think its a little hard for us (at least for me!) to figure out what would be the 'right' way go about coding this but if something like this ur trying to achive?
(compiles, NOT tested!)

int analogPin = A0;
bool timerStarted = false;
uint32_t timerStartMillis;

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(115200);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    while(1); //wait here forever
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  // Display static text
  display.println("Hello, world!");
  display.display();
}

void loop() {
  detectChanges();

  delay(200); //abitrary delay to allow us to see change on display ;)

  if (timerStarted) {
    // Display static text
    display.clearDisplay();
    display.println(millis() - timerStartMillis);
    display.display();
  }
}

void detectChanges() {
  digitalWrite(LED_BUILTIN, analogRead(A0));
  if (timerStarted == false && analogRead(A0) > 0) {
    timerStartMillis = millis();
    Serial.println("Start pump");
    display.clearDisplay();
    display.println("Pump Started!");
    display.display();
    timerStarted = true;
  }
  else if (timerStarted == true && analogRead(A0) < 1) {
    if (millis() - timerStartMillis > 500) {
      timerStartMillis = millis() - timerStartMillis;
      Serial.print("On time: ");
      Serial.println(timerStartMillis);
      Serial.println("Stop pump");
      timerStarted = false;
    }
  }
}

hope that helps...

If it's a timer, how does it increment?

Everything works fine with your help @sherzaad .i had to use another resistor for the input, but now flawless :smiley:
thx for your help

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