Go Down

Topic: Analog Input keyboard & switch bounce... (Read 851 times) previous topic - next topic


Dec 13, 2012, 11:25 pm Last Edit: Dec 13, 2012, 11:32 pm by squero Reason: 1
im new in arduino
i have arduino mega2560r3

my simple keyboard (6 push switches) give me a problem...
when i clik one of six switches my Led dont want Light, i must click on switch several times, and when i want Turn off my LED again i must click and click and click and click...and WOW now is Light off.

its my code

Code: [Select]

int sterowanie = 0;
//ustawianie pinow wyjsciowych
int out[6] = {30,32,34,36,38,40};
boolean przycisk[6] = {false,false,false,false,false,false};

void setup() {
  //pin polaczony z potencjometrem jest wejsciem
  pinMode(sterowanie, INPUT);

  //pin zapalajacy diode musi pracowac jako wyjscie
  for (int i=0; i<6; i++) {
    pinMode(out[i], OUTPUT);

void loop() {
  int ster = analogRead(sterowanie); //odczytujemy stan potencjometru

  //klikacz 1
  { //nastepnie porownujemy jego stan z wybrana wartoscia progowa
    przycisk[0] = !przycisk[0]; //wartosc przeciwna do aktualnej

  // Klikacz 2

  if(ster>750 && ster<870) { //nastepnie porownujemy jego stan z wybrana wartoscia progowa
    przycisk[1] = !przycisk[1];

  // Klikacz 3

  if(ster>580 && ster<700) { //nastepnie porownujemy jego stan z wybrana wartoscia progowa
    przycisk[2] = !przycisk[2];

  // Klikacz 4

  if(ster>410 && ster<530) { //nastepnie porownujemy jego stan z wybrana wartoscia progowa
    przycisk[3] = !przycisk[3];

  // Klikacz 5

  if(ster>240 && ster<370) { //nastepnie porownujemy jego stan z wybrana wartoscia progowa
    przycisk[4] = !przycisk[4];

  // Klikacz 6

  if(ster>70 && ster<200) { //nastepnie porownujemy jego stan z wybrana wartoscia progowa
    przycisk[5] = !przycisk[5];

  // wy?wietla stany przycisk√≥w
  for (int i=0; i<6; i++) {
    digitalWrite(out[i], przycisk[i]);

i think so this is bounce switch problem, any can help my . How can i fix it?

thanks ! :)

Schematic is in link,  or  http://iv.pl/images/82488628109570859970.png


Did you calculate the expected analogRead() values or did you measure them with your Arduino?  If you calculated them you might want to write a sketch to analogRead() and print to Serial.  Then you can separate the buttons mid-way between the values.  For example, if you get the following values:

No button: 0
Button 1: 970
Button 2: 810
Button 3: 640
Button 4: 470
Button 5: 305

You might want to use the following code:
Code: [Select]

   if (ster > (970+810)/2) {
       // Button 1
   } else
   if (ster > (810+640)/2) {
       // Button 2
   } else
   if (ster > (640+470)/2) {
       // Button 3
   } else
   if (ster > (470+305)/2) {
       // Button 4
   } else
   if (ster > (305+0)/2) {
       // Button 5
   } else {
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


Code: [Select]
  pinMode(sterowanie, INPUT);
It's generally not a good idea to diddle with the serial pin(s).

The analog pins are INPUT only. Therefore, pinMode() not need to be called for analog pins, and, in fact, can not be called for analog pins.
The art of getting good answers lies in asking good questions.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131