Go Down

Topic: Button programming problems can anyone help? (Read 446 times) previous topic - next topic

PlasticOne

Feb 12, 2013, 10:29 pm Last Edit: Feb 13, 2013, 01:03 am by PlasticOne Reason: 1
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: [Select]
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;
  }
}

PaulS

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?

PlasticOne

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)

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

HazardsMind

#4
Feb 13, 2013, 12:10 am Last Edit: Feb 13, 2013, 12:13 am by HazardsMind Reason: 1
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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PlasticOne

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

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

holmes4

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

Go Up