I'm making a button project that need about 10 leds and 12 buttons,
I'm using an arduino uno, but there just arn't enough I/O pins, I'm fairly
new to arduino, so it may be that I just dont know to to properly wire one up and porperly code it to make it work perfectly.
The project is this:
The idea is to have a project box with about 10 buttons on the project box and little LEDs next to them, wherever the LEDs are placed, there will be pieces of paper indicating what each button represents next to the LEDs . The box would also have 2 buttons on the side of the project box, one would be used to turn on the leds that had their corresponding buttons pushed and the other
would reset the system so it could be used with different combonations.
This is my code,
Don't pay attention to the comments!
// this constant won't change:
const int buttonPin = 2;
const int buttonPin2 = 8;
const int buttonPin3 = 3;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
int buttonReset2 = 10;
// the pin that the pushbutton is attached to
int ledPin = 13;
int ledPin2 = 12;
int ledPin3 = 4;
int ledPin4 = 7;
int ledPin5 = 11;
int ledShow2 = 9;
// the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0;
int buttonPushCounter2 = 0;
int buttonPushCounter3 = 0;
int buttonPushCounter4 = 0;
int buttonPushCounter5 = 0;
// counter for the number of button presses
int ledShow = 0;
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonReset = 0;
// current state of the button
int lastButtonState = 0;
int lastButtonState2 = 0;
int lastButtonState3 = 0;
int lastButtonState4 = 0;
int lastButtonState5 = 0;
void setup() {
// initialize the button pin as a input:
pinMode(ledShow, INPUT);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonReset = digitalRead(buttonReset2);
ledShow = digitalRead(ledShow2);
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == 1) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
}
else {
// if the current state is LOW then the button
// wend from on to off:
delay(1);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 2 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// compare the buttonState to its previous state
if (buttonState2 != lastButtonState2) {
// if the state has changed, increment the counter
if (buttonState2 == 1) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter2++;
}
else {
// if the current state is LOW then the button
// wend from on to off:
delay(1);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState2 = buttonState2;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter2 % 2 == 0) {
digitalWrite(ledPin2, HIGH);
} else {
digitalWrite(ledPin2, LOW);
}
// compare the buttonState to its previous state
if (buttonState3 != lastButtonState3) {
// if the state has changed, increment the counter
if (buttonState3 == 1) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter3++;
}
else {
// if the current state is LOW then the button
// wend from on to off:
delay(1);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState3 = buttonState3;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter3 % 2 == 0) {
digitalWrite(ledPin3, HIGH);
} else {
digitalWrite(ledPin3, LOW);
}
// compare the buttonState to its previous state
if (buttonState4 != lastButtonState4) {
// if the state has changed, increment the counter
if (buttonState4 == 1) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter4++;
}
else {
// if the current state is LOW then the button
// wend from on to off:
delay(1);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState4 = buttonState4;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter4 % 2 == 0) {
digitalWrite(ledPin4, HIGH);
} else {
digitalWrite(ledPin4, LOW);
}
// compare the buttonState to its previous state
if (buttonState5 != lastButtonState5) {
if (buttonState5 == 1) {
buttonPushCounter5++;
}
else {
delay(1);
}
}
lastButtonState5 = buttonState5;
if (buttonPushCounter5 % 2 == 0) {
digitalWrite(ledPin5, HIGH);
} else {
digitalWrite(ledPin5, LOW);
}
if (buttonReset == 1 && buttonPushCounter > 0){
buttonPushCounter--;
}
else{
delay(1);
if (buttonReset == 1 && buttonPushCounter2 > 0){
buttonPushCounter--;
}
else{
delay(1);
if (buttonReset == 1 && buttonPushCounter3 > 0){
buttonPushCounter--;
}
else{
delay(1);
}
if (buttonReset == 1 && buttonPushCounter4 > 0){
buttonPushCounter--;
}
else{
delay(1);
if (buttonReset == 1 && buttonPushCounter5 > 0){
buttonPushCounter--;
}
else{
delay(1);
}
if (ledPin == 1 && ledShow == 0){
ledPin--;
if(ledPin == 0 && ledShow ==1){
ledPin++;
}
else{
delay(1);
}
}else{
}
delay(1);
if (buttonReset == 1 && buttonState == 1){
buttonState++;
}
else{
delay(1);
}
if (buttonReset == 1 && buttonState2 == 1){
buttonState++;
}
else{
delay(1);
}
if (buttonReset == 1 && buttonState3 == 1){
buttonState++;
}
else{
delay(1);
}
if (buttonReset == 1 && buttonState4 == 1){
buttonState++;
}
else{
delay(1);
}
if (buttonReset == 1 && buttonState5 == 1){
buttonState++;
}
else{
delay(1);
}
}
}
}
}
Do not pay attention to the comments!
