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:
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.
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.
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.
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;
}
}
}