I am trying to make a device which has two push buttons and one analog input. At first the serial monitor will show INITIALIZING, then it will show ready to begin if the measured pin is pressed. Then only it will start to measure if again pressed, then it will show ****************************** and if the reset pin is pressed, it will show reset. If again pressed then ------------------------ but the problem is, it is showing
*************
(analog value) at the same time.
This is my code.
int sensor = A0;
int A = 0;
int resetPin = 3;
int measurePin = 2;
int resetstatus = 0;
int measurestatus = 0;
boolean flag1 = true;
boolean flag2 = true;
long current = 0;
void setup() {
Serial.begin (9600);
pinMode (sensor, INPUT);
pinMode (resetPin, INPUT_PULLUP);
pinMode (measurePin, INPUT_PULLUP);
current = millis ();
}
void loop() {
while (current < 3000) {
Serial.println ("INITIALIZING");
current = millis ();
delay (200);
}
Serial.println ("Ready to begin");
delay (200);
resetstatus = digitalRead (resetPin);
measurestatus = digitalRead (measurePin);
if (resetstatus == LOW) {
flag1 = !flag1;
delay (50);
}
if (flag1 == HIGH) {
delay (50);
reset ();
}
if (flag1 == LOW) {
Serial.println ("------------------------------------");
}
delay (100);
if (measurestatus == LOW) {
flag2 = !flag2;
delay (50);
}
if (flag2 == HIGH) {
delay (50);
measure ();
}
if (flag2 == LOW) {
Serial.println ("***********************************");
digitalWrite (13, LOW);
}
delay (100);
}
void measure() {
A = analogRead (sensor);
Serial.println (A);
digitalWrite (13, HIGH);
}
void reset() {
Serial.println ("reset");
}
millis()
is used uncorrectly here. 'current < 3000` can only be true within the first 3s after the power up, (and also everytime 'millis()' overflows). If you only want to have it executed one time you should write itsetup()
. Also: What do you mean byat the same time
? – chrisl Jun 5 '18 at 12:29