Hi all,
as a newbie trying to build my first project, i'm not being able to solve one problem.
The code bellow works well, as a prototype
When manual_open button is pressed, it loops until stop_open == HIGH.
I want to press manual_button anytime and stop he loop (while), and set a flag, which enables that the procedure to be resumed when pressing manual_button again. Which would be the safest way to implement it?
Thx in advance,
PW
#include <EEPROM.h>
#include <LiquidCrystal.h>
#include <AccelStepper.h>
#include <Button.h>
Button automatic(8, LOW) ; /* digital pin 8 */
Button manual_open(9, LOW); /* digital pin 9 */
Button manual_close(10, LOW); /* digital pin 10 */
const int stop_open = 6; /* digital pin 6 */
const int stop_close = 7; /* digital pin 7 */
const int ldr_pin = 0; /* analog pin 0 */
int ldr_val = 0;
int operation_mode = 'auto'; /* auto, mclose, mopen, wait */
int pause_mode = 0; /* 0 1 */
int val;
int buttonstate;
const int ledAutomatic = 3;
const int ledOpen = 4;
const int ledClose = 5;
void setup() {
pinMode(stop_open, INPUT);
pinMode(stop_close, INPUT);
pinMode(13,OUTPUT);
pinMode(ledAutomatic, OUTPUT);
pinMode(ledOpen, OUTPUT);
pinMode(ledClose, OUTPUT);
Serial.begin(9600);
}
void loop() {
ldr_val = analogRead(ldr_pin);
check_mode();
switch (operation_mode) {
case 'auto':
Automatic_Mode();
break;
case 'mclose':
Manual_Close();
break;
case 'mopen':
Manual_Open();
break;
case 'wait':
break;
}
digitalWrite(ledAutomatic, LOW);
digitalWrite(ledOpen, LOW);
digitalWrite(ledClose, LOW);
}
void check_mode() {
int val = 0;
automatic.listen();
manual_open.listen();
manual_close.listen();
if (automatic.onChange()) operation_mode = 'auto';
if (manual_open.onChange()) operation_mode = 'mopen';
if (manual_close.onChange()) operation_mode = 'mclose';
}
void Automatic_Mode() {
if (ldr_val < 600) {
digitalWrite(13, HIGH);
/* open blinds */
Serial.print("Automatic Open\n\r"); }
else {
digitalWrite(13, LOW);
/* close blinds */
Serial.print("Automatic Close\n\r"); }
digitalWrite(ledAutomatic, HIGH);
delay(100);
}
void Manual_Open() {
while(digitalRead(stop_open) == LOW && operation_mode == 'mopen') {
Serial.print("manual_open ");
Serial.print(pause_mode);
Serial.print("\r\n");
digitalWrite(ledOpen, HIGH);
check_mode();
}
digitalWrite(ledOpen, LOW);
Serial.print("manual_open ");
Serial.print(pause_mode);
Serial.print("\r\n");
operation_mode = 'stop';
}
void Manual_Close() {
while(digitalRead(stop_close) == LOW && operation_mode == 'mclose') {
digitalWrite(ledClose, HIGH);
check_mode();
}
digitalWrite(ledOpen, LOW);
operation_mode = 'stop';
}