I am particularly excited about project 02 not only because it was named Spaceship Interface but also because this is the first project where we will write code for the Arduino Uno board.
If you haven't gotten your Arduino IDE installed yet follow the official guide.
Requirements
- Three LEDs (1 green, 2 red)
- Three 220 ohm resistor
- 10 kilohm resistor
- Switch
The description for project 02 is as follows;
You'll be building something that could have been a spaceship interface in a 1970's science fiction movie. You'll make a cool control panel with a switch and lights that turn on when you press the switch. You can decide whether the lights mean "Engage Hyperdrive" or "Fire the lasers!". A green LED will be on, until you press a button. When the Arduino gets a signal from the button, the green light will turn off and 2 other lights will start blinking.
Here are a few things to note as you make connections to your breadboard;
- The longer leg of the LED is the anode (+ve) while the shorter leg is the cathode (-ve).
- The button should be placed at the center of the breadboard.
Here's a view of the breadboard and Uno
Here's a schematic of the circuit
I took a course in school in my 5th semester called Digital Electronics and Logic Design, in that course I learnt about system states of digital systems which could be LOW or HIGH, INPUT or OUTPUT, 0 or 1.
Each program in the Arduino IDE is called a sketch. A sketch has two required functions for it to work the setup()
and the loop()
.
Before declaring the functions we define variables, the syntax seems similar to C++ (I say C++ because that's a language I recently started learning) where we define variables by variable_type variable_name = variable;
The setup()
function runs only once, here we design the state of the digital pins to be inputs or outputs using the pinMode()
function. In this case our button is input.
The loop()
function runs continuously as the name implies. The loop is where the bulk of the program lies, this is where we check for;
- The voltage of the input using the
digitalRead()
function which checks the chosen pin passed as an argument for voltage. In this case we store it in the switchState variable. - If there is a voltage on the pin, switchState will have a value of 1 which is HIGH, if there is no voltage switchState will have a value of 0 which is LOW. If switchState is LOW it means the button
- We use the
digitalWrite()
function to send voltage to a pin either HIGH or LOW. HIGH would be the voltage we have selected on our board (5V in this case) and LOW would be 0V.digitalWrite()
takes two arguments, the pin and what value to give it. - The else statement is there if switchState is HIGH, then we set the green LED and one red LED to LOW, we delay and toggle the red LEDs.
The code we'll be uploading to our Board;
int switchState = 0;
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
switchState = digitalRead(2);
if (switchState == LOW ) {
// the button is not pressed
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);}
else {
// the button is pressed
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(250);
}
}
The steps to upload the program to your board;
- Save your sketch
From the menubar, click tools;
- Make sure you have the right board selected
- Select a port
- Set the programmer to ArduinoISP
- The upload button is the right arrow.
The output:
The cover below came with the Arduino Starter Kit.
References
- Arduino Projects Book