0

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");
}
7
  • The while statement with 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 it setup(). Also: What do you mean by at the same time? – chrisl Jun 5 '18 at 12:29
  • change your pin declarations to #define sensor A0 ect.... there is no reason to have them be variables. the variable current should be an unsigned long, not just a long. Also no need to use millis() for your init portion. In your setup loop you can just do Serial.println("Init"); delay(3000); – Chad G Jun 5 '18 at 16:48
  • Still not sure what your problem is. Can you post the output – Chad G Jun 5 '18 at 16:52
  • @chrisl i wanted the INITIALIZATION to be showed only for 3secs when the power is turned on – Peouse Dutta Jun 6 '18 at 11:19
  • @ChadG i want to observe only analog value when the measured button is pressed and reset to be shown when the reset button is pressed – Peouse Dutta Jun 6 '18 at 11:22
2

You have declare flag1 and flag2 as Boolean values but then you are looking to see if they are HIGH or LOW. Bools are either true or false. However because of the quirks of C++ this will probably work, if High and low are 1 and 0, its just bad style.

When you have this bit of code:

if (flag2 == HIGH)
{
    delay (50);
    measure ();
}
if (flag2 == LOW)
{
    Serial.println ("***********************************");
    digitalWrite (13, LOW);
}

You could use an else statement instead, i.e.

if (flag2 == true) // Corrected to true
{
    delay (50);
    measure ();
}
else
{
    Serial.println ("***********************************");
    digitalWrite (13, LOW);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.