Pages: [1]   Go Down
Author Topic: Analog Input keyboard & switch bounce...  (Read 390 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey
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:
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
  if(ster>920)
  { //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 ! smiley

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


* klawa.png (10.26 KB, 614x505 - viewed 8 times.)
« Last Edit: December 13, 2012, 05:32:05 pm by squero » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8084
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
   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 {
        // NO BUTTON PRESSED
    }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46154
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Pages: [1]   Go Up
Jump to: