Hello,
I am missing something in basic code below. You might see it.
It seems something is wrong in the function int debounceIN0(). The variable ton0_sb should detect high to low edge transition on input IN0
and shall be set forever to 0x02 if button is pressed.
Expected output on uart:
ton0_sb = 2
lastFallingEdgeTime0 = like 123456
Real output on uart:
ton0_sb = 0
lastFallingEdgeTime0 = 0
// IO assigment:
// there are two inputs IN0, IN1 where are buttons connected, button active is LOW
// there are two outputs OUT1, OUT2 which are connected to two relays, assuming OUT1 runs motor forward, OUT2 runs motor backward
//
// the aim of logic2()
// + aim of this is to ensure the hardware wiring is correct
// + IN0 press runs motor forward, it tests falling edge happend
// + IN1 press runs motor backward, it tests falling edge happend
//
// target arduino mega attiny core, chip attiny1614, shall work on Uno R3 as well
//
#define IN0 (0)
#define IN1 (1)
#define OUT0 (2)
#define OUT1 (3)
// datatype
enum mem_
{
BOTH_OFF = 0x00,
OUT0_ON = 0x01,
OUT1_ON = 0x02
};
enum ton_sb
{
OFF = 0x00,
RUN = 0x02,
CPL = 0x04
};
void setup_io(void);
int debounceIN0(void);
int debounceIN1(void);
void logic2(void);
void dbg_msg(void);
// Duration for which OUT0 and OUT1 remain HIGH
// const unsigned long onDuration = 1000; // 1 second
// Debounce time in milliseconds
const unsigned long debounceDelay = 20;
// Variables for edge detection, debouncing, and timing
unsigned long lastFallingEdgeTime0 = 0;
unsigned long lastFallingEdgeTime1 = 0;
unsigned long lastDebounceTime0 = 0;
unsigned long lastDebounceTime1 = 0;
int lastButtonState0 = HIGH; // Start assuming button is not pressed
int lastButtonState1 = HIGH; // Start assuming button is not pressed
int buttonState0;
int buttonState1;
enum ton_sb ton0_sb; // timer0 status byte
enum ton_sb ton1_sb; // timer1 status byte
void setup()
{
setup_io();
Serial.begin(19200);
}
void loop()
{
// Call debounce functions
debounceIN0();
debounceIN1();
/* logic1(); will be implemented later */
logic2();
dbg_msg();
}
void dbg_msg(void)
{
static uint16_t dbg_time, dbg_last_time;
// prints every second variable under investigation
dbg_time = millis();
if (dbg_time - dbg_last_time < 1000)
{
}
else
{
dbg_last_time = dbg_time;
Serial.print("ton0_sb = ");
Serial.println(ton0_sb);
Serial.print("lastFallingEdgeTime0 = ");
Serial.println(lastFallingEdgeTime0);
}
}
// this shall set OUT0 output forever HIGH if ton0_sb is set to RUN
void logic2(void)
{
enum mem_ mem = BOTH_OFF;
// network:
// if (buttonState0==LOW) <-- this works if uncommented
if (ton0_sb==RUN) /* <-- this variable seems that is always 0 */
{
mem = OUT0_ON;
}
// network:
// if (buttonState1==LOW) <-- this works if uncommented
if (ton1_sb==RUN) /* <-- this variable seems that is always 0 */
{
mem = OUT1_ON;
}
// network: finally write to output
switch (mem)
{
case OUT0_ON:
digitalWrite(OUT0, HIGH);
break;
case OUT1_ON:
digitalWrite(OUT1, HIGH);
break;
default:
digitalWrite(OUT0, LOW);
digitalWrite(OUT1, LOW);
}
}
// Debounce function for IN0
int debounceIN0()
{
int reading = digitalRead(IN0);
if (reading != lastButtonState0)
{
lastDebounceTime0 = millis();
}
if ((millis() - lastDebounceTime0) > debounceDelay)
{
if (reading != buttonState0)
{
buttonState0 = reading;
if (buttonState0 == LOW && lastButtonState0 == HIGH)
{
if (ton0_sb != RUN)
{
lastFallingEdgeTime0 = millis();
ton0_sb = RUN;
}
}
}
}
lastButtonState0 = reading;
return buttonState0;
}
// Debounce function for IN1
int debounceIN1()
{
int reading = digitalRead(IN1);
if (reading != lastButtonState1)
{
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay)
{
if (reading != buttonState1)
{
buttonState1 = reading;
if (buttonState1 == LOW && lastButtonState1 == HIGH)
{
if (ton1_sb != RUN)
{
lastFallingEdgeTime1 = millis();
ton1_sb = RUN;
}
}
}
}
lastButtonState1 = reading;
return buttonState1;
}
void setup_io(void)
{
// Initialize IN0 and IN1 as inputs with pull-up resistors
pinMode(IN0, INPUT_PULLUP);
pinMode(IN1, INPUT_PULLUP);
// Initialize OUT0 and OUT1 as outputs
pinMode(OUT0, OUTPUT);
pinMode(OUT1, OUTPUT);
// Ensure outputs start LOW
digitalWrite(OUT0, LOW);
digitalWrite(OUT1, LOW);
}
// EOF