push button with led matrix

Post on 12-Apr-2017

167 views 1 download

Transcript of push button with led matrix

Demonstrating multi-dimensional array using 9 LEDs

You will need...

• Arduino Board (UNO)• 9 LEDs• 9 Resistors - 220Ω• Jumper Wires• Breadboard

Declaring, Initializing and Referencing a matrix:

Program

int pinMatrix[3][3]=5,6,7,8,9,10,11,12,13;void setup() for(int i=0;i<3;i++) for(int j=0;j<3;j++) pinMode(pinMatrix[i][j],OUTPUT);

Cont…void loop() for(int i=0;i<3;i++) for(int j=0;j<3;j++) digitalWrite(pinMatrix[i][j],HIGH); delay(20); digitalWrite(pinMatrix[i][j],LOW);

working with push buttons

Switch vs Push button

A "switch" is a binary device with an "on" and "off" position.You can toggle it between on and off positions.Can be implemented to on switch on and off an led manually. A "button" is a binary device with a momentary "on" position ,and reverts back to “off" position immediately. It cannot be toggled, but pushed multiple times.Can be implemented to count the number of votes ,increase the brightness of led manually.

Connections

Cont…Botton:•Leave 2 terminals open•One of the terminal to 5v•second terminal to 1k resistor and ground•Other terminal of resistor to arduino digital pinLed:•anode to ground•Cathode to arduino digital pin through 220 ohm resistor

Switch on a LEDint ledPin=8;int pbPin=7; int val=0; void setup() pinMode(ledPin,OUTPUT); pinMode(pbPin,INPUT); void loop() val= digitalRead(pbPin); if (val==HIGH) digitalWrite(ledPin,HIGH); else digitalWrite(ledPin,LOW);

To count no of button pressesint pbPin=7; int count=0;void setup() pinMode(pbPin,INPUT); Serial.begin(9600);void loop() if (digitalRead(pbPin)==HIGH) count++; Serial.print("students present="); Serial.print(count); Serial.print("\n"); else ;

Problem encountered

For a single button press based on the duration ,the output will be as shown.

Modified codeint count=0;int lastState=LOW;void setup() Serial.begin(9600); pinMode(7,INPUT); void loop() if(digitalRead(7)==HIGH&&lastState==LOW) count++; Serial.print("students present="); Serial.print(count); Serial.print("\n"); lastState=digitalRead(7);

Output for 19 button presses.