This week, we built upon a code we started during class. Originally, we we able to count the number of clicks, visually the value was seen by using a print command and boolean. This was done using one button and a resistor. For homework, I had to wire three buttons and resistors with my breadboard and Arduino. The code developed assigned a different function to each button. One button will change the counter value by adding one. A second button decreases the counter value by a change of subtracting one. The last and third button is a 'reset' button that changes the value to 0.
Shared below is my code and comments:
__________________________________
int counter = 0; //defining the counter, from previous work
int butt3 = 4; // button three, meaning it is in pin 4 on Arduino board
int butt2 = 3; // button two, meaning it is in pin 3 on Arduino board
int butt1 = 2; // button one, meaning it is in pin 2 on Arduino board
int buttValue3; // I understand this step of button values to be defining a (set of) variable(s) that can be given value(s)
int buttValue2; // ...as above...
int buttValue1; // ...as above...
// these -int- above are now all universally "defined" and applied / "callable" from the written code below...
// "butt" is a shorthand for "button"
void setup() {
pinMode(butt1, INPUT);
pinMode(butt2, INPUT);
pinMode(butt3, INPUT);
Serial.begin(9600);
}
void loop() {
buttValue1 = digitalRead(butt1);
buttValue2 = digitalRead(butt2);
buttValue3 = digitalRead(butt3);
if (buttValue3 == 1) {
counter = 0;
Serial.println(counter);
}
if (buttValue2 == 1) {
counter = counter - 1;
Serial.println(counter);
}
if (buttValue1 == 1) {
counter = counter + 1;
Serial.println(counter);
}
delay(250);
}