|
|
|
[**Arduino IDE File**](https://gitlab.hsrw.eu/28578/digitalfab/-/raw/main/Week%205/sketch_nov20a.ino?inline=false)
|
|
|
|
|
|
|
|
### Assignment
|
|
|
|
|
|
|
|
3 LEDs need to be controlled by at most two buttons (one button is ok as well)
|
|
|
|
|
|
|
|
### Required Parts
|
|
|
|
|
|
|
|
- 3 LED lights (if possible in different colors)
|
|
|
|
- 1 button
|
|
|
|
- 1 breadboard
|
|
|
|
- 1 Arduino / Raspberry Pi
|
|
|
|
- 4 resistors (3x around 300 Ohm, 1x 10k Ohm)
|
|
|
|
- cables
|
|
|
|
|
|
|
|
### Tinkercad
|
|
|
|
|
|
|
|
The assignment will first be tested within TinkerCad. This setup will then be used to build a real life model.
|
|
|
|
|
|
|
|
**Setup:**
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
The three LED-lights (red, yellow, green) and the button are placed onto the breadboard. The lights and the button are connected to ground via the resistors. The pink cable of the red LED is connected to port 13, the yellow cable of the yellow LED is connected to port 12 and the green cable of the green LED is connected to port 8. The button is connected to port 4. The red and black cables connect to the ACC and ground respectively.
|
|
|
|
|
|
|
|
**Code:**
|
|
|
|
The functionality of the LEDs and the button is specified within the code.
|
|
|
|
|
|
|
|
```
|
|
|
|
const int redLED = 13;
|
|
|
|
const int yelLED = 12;
|
|
|
|
const int greenLED = 8;
|
|
|
|
const int button = 4;
|
|
|
|
int buttonState = 0;
|
|
|
|
int redState = 0;
|
|
|
|
int yelState = 0;
|
|
|
|
int greenState = 0;
|
|
|
|
int ledNr = 0;
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(9600);
|
|
|
|
pinMode(redLED, OUTPUT);
|
|
|
|
pinMode(yelLED, OUTPUT);
|
|
|
|
pinMode(greenLED, OUTPUT);
|
|
|
|
pinMode(button, INPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// put your main code here, to run repeatedly:
|
|
|
|
buttonState = digitalRead(button);
|
|
|
|
redState = digitalRead(redLED);
|
|
|
|
yelState = digitalRead(yelLED);
|
|
|
|
greenState = digitalRead(greenLED);
|
|
|
|
|
|
|
|
if (buttonState == 1) {
|
|
|
|
Serial.println("Button pressed");
|
|
|
|
Serial.println(ledNr);
|
|
|
|
switch (ledNr) {
|
|
|
|
case 0:
|
|
|
|
redState = controlLED(redLED, redState);
|
|
|
|
delay(200);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
redState = controlLED(redLED, redState);
|
|
|
|
ledNr = 1;
|
|
|
|
yelState = controlLED(yelLED, yelState);
|
|
|
|
delay(200);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
redState = controlLED(redLED, redState);
|
|
|
|
delay(200);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
redState = controlLED(redLED, redState);
|
|
|
|
ledNr = 3;
|
|
|
|
yelState = controlLED(yelLED, yelState);
|
|
|
|
ledNr = 3;
|
|
|
|
greenState = controlLED(greenLED, greenState);
|
|
|
|
delay(200);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
redState = controlLED(redLED, redState);
|
|
|
|
delay(200);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
redState = controlLED(redLED, redState);
|
|
|
|
ledNr = 5;
|
|
|
|
yelState = controlLED(yelLED, yelState);
|
|
|
|
delay(200);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
redState = controlLED(redLED, redState);
|
|
|
|
delay(200);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Serial.println("default");
|
|
|
|
digitalWrite(redLED, LOW);
|
|
|
|
digitalWrite(yelLED, LOW);
|
|
|
|
digitalWrite(greenLED, LOW);
|
|
|
|
delay(200);
|
|
|
|
ledNr = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int controlLED(int currentLED, int currentState) {
|
|
|
|
if (currentState == 0) {
|
|
|
|
digitalWrite(currentLED, HIGH);
|
|
|
|
} else {
|
|
|
|
digitalWrite(currentLED, LOW);
|
|
|
|
}
|
|
|
|
currentState = digitalRead(currentLED);
|
|
|
|
ledNr++;
|
|
|
|
return currentState;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
All variables are declared. The LEDs are assigned to the respective ports. Their state is set to 0. The button is assigned to its port and the state is also set to 0. Lastly, a counter for the different LED states is introduced.
|
|
|
|
The pin mode for all the LEDs is set to `OUTPUT`, the button is set to `INPUT`.
|
|
|
|
|
|
|
|
When the button is pressed the code enters a switch block. Depending on the LED counter it enters a different case. With a function, that turns the LEDs on and off the different possibilities of which lights are turned on and off are gone through.
|
|
|
|
|
|
|
|
If we look at the different LEDs like binaries, the logic is that the LEDs count up with every press of the button. So the different states are:
|
|
|
|
```
|
|
|
|
000
|
|
|
|
001
|
|
|
|
010
|
|
|
|
011
|
|
|
|
100
|
|
|
|
101
|
|
|
|
111
|
|
|
|
```
|
|
|
|
The ones represent the lit up LED lights.
|
|
|
|
In TinkerCad this is currently the other way around.
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
### Model with a Raspberry Pi
|
|
|
|
|
|
|
|
This version is modeled after the first draft in TinkerCad.
|
|
|
|
The resistors for the LEDs are 330 Ohm and the resistor for the button is 10k Ohm.
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
The Code is the exact same as well.
|
|
|
|
The Raspberry Pi board needed to be added to the Arduino IDE. For that this link was added to the preferences: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
Then Waveshare RP2040 zero was selected as the board.
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
With a click on upload the code was transferred to the Raspberry Pi.
|
|
|
|
|
|
|
|
For the LEDs and the button I used the same ports as online in TinkerCad.
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
The final project:
|
|
|
|
|
|
|
|
 |
|
|
|
\ No newline at end of file |