Go Down

Topic: Help! (Read 230 times) previous topic - next topic

BryceTech

I have my project hooked up to two buttons 2 and 3, The code is checking for an update on the two buttons then it displays it in the serial monitor D1 (Door 1) works fine but when I press D2 (Door 2) it displays the text for D1 and D2

This is my code:

int D1 = 2;
int D2 = 3;

int D1state = 0;
int lastD1state = 0;

int D2state = 0;
int lastD2state = 0;

void setup() {
  //Start Serial
Serial.begin (9600);
  // Initalize Door D1

pinMode(D1, INPUT);
pinMode(D2, INPUT);

Serial.print(">Executing startup.sh");
 delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (500);
Serial.print(">Executing libaries.sh");
 delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (500);
Serial.print(">Executing buttons.sh");
 delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (500);
  Serial.println(" ");
Serial.println(">Ready");
Serial.println(" ");
}

void loop() {
  // read the pushbutton input pin:
  D1state = digitalRead(D1);
  D2state = digitalRead(D2);
  // compare the buttonState to its previous state
  if (D1state != lastD1state) {
    // if the state has changed, increment the counter
    if (D1state == HIGH) {
    Serial.println("--------------------------");
  Serial.print(">Executing open_door1.sh");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (500);
 
  Serial.print(">Initiating");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (1500);
 
  Serial.println(">Success!");
  Serial.println("--------------------------");
  Serial.println(" ");
  delay (1000);
 
  Serial.println(">Ready"); // Ready to receive commands
  Serial.println(" ");
    }


   if (D2state != lastD2state) {
    // if the state has changed, increment the counter
    if (D2state == HIGH) {
    Serial.println("--------------------------");
  Serial.print(">Executing open_door2.sh");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (500);
 
  Serial.print(">Initiating");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (1500);
 
  Serial.println(">Success!");
  Serial.println("--------------------------");
  Serial.println(" ");
  delay (1000);
 
  Serial.println(">Ready"); // Ready to receive commands
  Serial.println(" ");
    }

     
    }
  }
  }


AWOL

Code: [Select]
Serial.print(">Executing startup.sh");
 delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (500);
Serial.print(">Executing libaries.sh");
 delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (500);
Serial.print(">Executing buttons.sh");
 delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (500);
  Serial.println(" ");
Serial.println(">Ready");
Serial.println(" ");
What's all that bollocks?
Where are the code tags?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

BryceTech

#2
Mar 29, 2017, 11:04 pm Last Edit: Mar 29, 2017, 11:11 pm by BryceTech
Oh, Its the "Startup" procedure its meant to replicate a AI startup, Its just aesthetics

J-M-L

If you press ctrl-T in the IDE (or cmd-T on a Mac) you'll see your bug

Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages priv├ęs SVP

BryceTech

If you press ctrl-T in the IDE (or cmd-T on a Mac) you'll see your bug


That didnt fix it

AWOL

He didnt say "fix it", he said "see"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

AWOL

Its just aesthetics
I've never seen "bollocks" spelled like that.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

BryceTech

I am working on a project that opens and closes doors. There are two buttons hooked up D1 is hooked up to pin 2. D2 is connected to pin 3. The code right now is supposed to detect a change in the state of the buttons and when a change is detected display is in the serial monitor. when I press D1 it works completely fine. But when I press D2 it Displays the text for D1 first and also rind D2 text when it is supposed to only display D2 text with pressing D2 and D1 text with pressing D1.

Code: [Select]

int D1 = 2;
int D2 = 3;

int D1state = 0;
int lastD1state = 0;

int D2state = 0;
int lastD2state = 0;

void setup() {
  //Start Serial
  Serial.begin (9600);
  // Initalize Door D1

  pinMode(D1, INPUT);
  pinMode(D2, INPUT);

  Serial.print(">Executing startup.sh");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (500);
  Serial.print(">Executing libaries.sh");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (500);
  Serial.print(">Executing buttons.sh");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.print(".");
  delay (500);
  Serial.println(".");
  delay (500);
  Serial.println(" ");
  Serial.println(">Ready");
  Serial.println(" ");
}

void loop() {
  // read the pushbutton input pin:
  D1state = digitalRead(D1);
  D2state = digitalRead(D2);
  // compare the buttonState to its previous state
  if (D1state != lastD1state) {
    // if the state has changed, increment the counter
    if (D1state == HIGH) {
      Serial.println("--------------------------");
      Serial.print(">Executing open_door1.sh");
      delay (500);
      Serial.print(".");
      delay (500);
      Serial.print(".");
      delay (500);
      Serial.println(".");
      delay (500);

      Serial.print(">Initiating");
      delay (500);
      Serial.print(".");
      delay (500);
      Serial.print(".");
      delay (500);
      Serial.println(".");
      delay (1500);

      Serial.println(">Success!");
      Serial.println("--------------------------");
      Serial.println(" ");
      delay (1000);

      Serial.println(">Ready"); // Ready to receive commands
      Serial.println(" ");
    }


    if (D2state != lastD2state) {
      // if the state has changed, increment the counter
      if (D2state == HIGH) {
        Serial.println("--------------------------");
        Serial.print(">Executing open_door2.sh");
        delay (500);
        Serial.print(".");
        delay (500);
        Serial.print(".");
        delay (500);
        Serial.println(".");
        delay (500);

        Serial.print(">Initiating");
        delay (500);
        Serial.print(".");
        delay (500);
        Serial.print(".");
        delay (500);
        Serial.println(".");
        delay (1500);

        Serial.println(">Success!");
        Serial.println("--------------------------");
        Serial.println(" ");
        delay (1000);

        Serial.println(">Ready"); // Ready to receive commands
        Serial.println(" ");
      }
    }
  }
}



KeithRB


Gabriel_swe

How is your wiring? Straight from 5 V to hour pin? I suspect your pin is floating and you get false readings.

You also have lots of delays giving you a very laggy experience. Remove and / or reduce some of them for a more responsive code.

johnwasser

You forgot to set lastD1state and lastD2state anywhere in loop().
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

evanmars

Where do you update lastD1state and lastD2state?

BryceTech

I have figured out how to update both buttons but randomly nothing happens when I press either button but sometimes it works flawlessly.

Code: [Select]

const int  switchOnePin = 2;    // digital in 2 (pin the switch one is attached to)
const int  switchTwoPin = 3;    // digital in 3 (pin the switch two is attached to)

int switchOneState = 0;         // current state of the switch
int lastSwitchOneState = 0;     // previous state of the switch

int switchTwoState = 0;
int lastSwitchTwoState = 0;

void setup() {
  //initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  int switchOneState = 0;         // current state of the switch
  int lastSwitchOneState = 0;     // previous state of the switch switch pins as an input
  pinMode(switchOnePin, INPUT);
  pinMode(switchTwoPin, INPUT);
  Serial.println(">Ready");
  Serial.println(" ");
}

void loop() {
  // read the switch input pins:
  switchOneState   = digitalRead(switchOnePin);
  switchTwoState   = digitalRead(switchTwoPin);

  // compare the switchState to its previous state
  if (switchOneState != lastSwitchOneState) {
    // if the state has changed, increment the counter
    if (switchOneState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      Serial.println("Switch one is on");
    } else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("Switch one is off");
      Serial.println(" ");
      Serial.println(">Ready");
      Serial.println(" ");
    }
  }

  if (switchTwoState != lastSwitchTwoState) {
    if (switchTwoState == HIGH) {
      Serial.println("Switch two is on");
    } else {
      Serial.println("Switch two is off");
      Serial.println(" ");
      Serial.println(">Ready");
      Serial.println(" ");
    }
  }

  // Delay a little bit to avoid bouncing
  delay(50);

  // save the current state as the last state,
  //for next time through the loop
  lastSwitchOneState   = switchOneState;
  lastSwitchTwoState   = switchTwoState;
}


jremington

#13
Mar 30, 2017, 04:58 am Last Edit: Mar 30, 2017, 05:00 am by jremington
Buttons "bounce". You should read up on button debouncing techniques, or use one of the available libraries.

It is not nearly as simple to handle (i.e. with a 50 ms delay) as you seem to think.

Coding Badly


@BryceTech, stop cross-posting.  Threads merged.


Go Up