Pages: [1]   Go Down
Author Topic: Button programming problems can anyone help?  (Read 351 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Little bit stuck cant seem to get the button to work right it only seems to work some of the time and if held down just skips throw the modes anyone help?
Code:
const int redledPin = 2;
const int greenledPin = 4;
const int blueledPin = 5;
const int buttonPin1 = 6;
int led=1;

void setup ()
{
  pinMode (redledPin, OUTPUT);
  pinMode (greenledPin, OUTPUT);
  pinMode (blueledPin, OUTPUT);
  pinMode (buttonPin1, INPUT);
}

void loop ()
{
 if (digitalRead(buttonPin1)== HIGH)
{
  led++;
  if (led >16) led = 1;
  delay(300);
}
  
   switch (led){
    case 0:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    break;    
    
    case 1:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    break;
    
    case 2:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    break;
    
    case 3:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    break;
    
    case 4:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    break;
    
    case 5:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    break;
    
    case 6:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    break;
    
    case 7:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    break;
    
    case 8:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    delay(100);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
    
    case 9:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
    
    case 10:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(100);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
    
    case 11:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(100);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
    
    case 12:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    delay(100);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
    
    case 13:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(100);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
    
    case 14:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(100);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
    
    case 15:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    delay(100);
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(100);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(100);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    delay(100);
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(100);
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(100);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   }
}
« Last Edit: February 12, 2013, 07:03:13 pm by PlasticOne » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good thing you found the problem before writing a ton of improperly posted code.

You are not using the internal pullup resistor, so, how IS your switch wired?
Logged

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

Just using a stranded push switch and the switch is wired one side into 5v with resistor into ground and the other side into pin 6 (hope that made sens i'm really new at this)
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3453
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is doing exactly what you programmed to do.  You hold the button down, led gets incremented and so it goes on.  What do you want it to do ?  I expect that you want it to increment led each time the button is pressed.  In which case you need to ignore the button being held down (same value from digitalRead each time you check) and note when the button has been released and wait for it to be pressed again.  You will also need to debounce the button at some time too in order to avoid false readings.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

In short, button state change and debounce. Unless you can debounce and latch the button via hardware, look into those examples provided to you in the arduino software.
« Last Edit: February 12, 2013, 06:13:17 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

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

Thanks guys will take a crack at that and let you no how i get on : )
Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11154
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the switch is wired one side into 5v with resistor into ground and the other side into pin 6

Maybe I'm just misunderstanding you, but that doesn't sound right.

I suggest you enable the internal pull-up resistor on the input pin, and connect the switch between the pin and ground. Then the input will read HIGH when the switch is open and LOW when it is closed.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For future reference:

Read this before posting a programming question


Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.


Code tags, not monospaced font.
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1872
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to de-bounce the button (see examples). Also take a look at the use of digitalWrite(pin,HIGH|LOW) don't use analogWrite() just to turn LED's on and off.

Also take a look at "blink without delay()" and the use of functions in general.

Mark
Logged

Pages: [1]   Go Up
Jump to: