Pages: 1 [2]   Go Down
Author Topic: Program stops after button press  (Read 1269 times)
0 Members and 1 Guest are viewing this topic.
Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try this
Code:
uint8_t Switch = 2;
uint8_t Led =13;

boolean LedState =LOW;
int SwitchState =0;
int SwitchDebounce;
int LastSwitchState=HIGH;
int LastSwitchDebounce=LOW;

unsigned long LastDebounceTime = 0;
unsigned long DebounceDelay = 50;

void setup()
{
  pinMode(Switch,INPUT);
  digitalWrite(Switch,HIGH);
  pinMode(Led,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int CurrentSwitch = digitalRead(Switch);
  if (CurrentSwitch != LastSwitchDebounce)
  {
    LastDebounceTime = millis();
  }
  if ((millis() - LastDebounceTime) > DebounceDelay)
  {
    if (CurrentSwitch != LastSwitchState)
    {
      if (CurrentSwitch == LOW)
      {
        CountNumber++;
      }
    }
    LastSwitchState=CurrentSwitch;
  }
  if ( !CurrentSwitch && !LastSwitchState) CountNumber++;
  Serial.println(CountNumber);
  LastSwitchDebounce = CurrentSwitch;
}

Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3766
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@ash901226
No, he wants this.

Code:
const int buttonPin = 2;     

int buttonState = 0;
int  lastReading = 0;
long onTime=0;
int count = 0;

void setup() {
  pinMode(ledPin, OUTPUT);     
  pinMode(buttonPin, INPUT);
  Serial.begin(9600); 
}

void loop(){
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH && lastReading == LOW) { // single press
    onTime = millis();
    count++;
  }

  //held
  if (buttonState == HIGH && lastReading == HIGH) { //single press + hold
    if ((millis() - onTime) > 500 ) { // looks to see if button is pressed for more than half a second.
      delay(200); // **the rate in which the numbers increase**
      count++;
      lastReading = LOW;
    }
  }
  Serial.println(count);
  lastReading = buttonState;
}

« Last Edit: February 05, 2013, 08:28:03 am by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code presented doesn't work.
Right now, the code I have is perfect, because that works perfectly for everything (keeping it pressed etc).
The only problem now must be in the circuit. Because when I press 1 of the buttons or both, the LED flashes faaaaar more brightly.
 So I think that somewhere by pressing the button, the circuit gets closed and therefore more voltage flowsthrough the LED. I have no electronical knowledge, so there is my problem smiley-razz.
First I thought I didn't need a branch from the 5V for the LED or for the buttons, but I need it, or I have no parallel I think.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have found what the problem was in the end.
The problem was that the LED was turned on and off too fast after another.
What I mean by that is that by the time the LED was set FROM HIGH to LOW, it barely had gotten time to go HIGH, resulting in only a super small blink.
I have added a delay which does not affect my timer code.
The sleep could not be bigger or equal than 60000 (ms) / BPM.

The resulting code:
Code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int BPM = 120;
int minSwitchState;
int plusSwitchState;
float toDelay;
long previousMillis = 0;
boolean buttonPressed;
int counter = 0;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  updateDisplay();

  //red output LED
  pinMode(13, OUTPUT);
  //green output LED
  pinMode(10, OUTPUT);

  //input buttons
  pinMode(7, INPUT);
  pinMode(8, INPUT);
}

void loop() {
  buttonPressed = false;
  minSwitchState = digitalRead(8);
  plusSwitchState = digitalRead(7);
 
  if(minSwitchState == HIGH) {
    buttonPressed = true;
    BPM++;
    updateDisplay();
  }
  if(plusSwitchState == HIGH) {
    buttonPressed = true;
    if(BPM > 1) {
      BPM--;
      updateDisplay();
    }
  }
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= toDelay && !buttonPressed) {
    counter = counter % 4 + 1;
    if(counter == 1) {
      digitalWrite(10, HIGH);
    }else{
      digitalWrite(13, HIGH);
    }
    delay(100);
    previousMillis = currentMillis;
    Serial.println(currentMillis / 1000.0);
  }else{
    digitalWrite(13, LOW);
    digitalWrite(10, LOW);
  }
}

void updateDisplay() {
  toDelay = 60000.0 / (float) BPM;
  lcd.clear();
  lcd.print("Kies BPM: ");
  lcd.setCursor(0, 1);
  lcd.print(BPM);
  delay(100);
}

Thanks a lot all, this can be put to [SOLVED] smiley!
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3766
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There was something wrong with my code?, I thought it did EXACTLY what you wanted. You press it once, it increments once, you press and hold it and it increments at the delay(200) rate.

Ok, well if you got it to work on your own, then thats even better. You found the problem and was able fixed it.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Pages: 1 [2]   Go Up
Jump to: