This project follows the Spaceship Interface tutorial from the Arduino projects book. I chose to type out the code manually to better understand the language and to prepare me to better troubleshoot in the future.
I did make a small modification to the code Arduino provides. I created a new variable for the delay time. I wanted to modify Arduino’s recommendation of 250ms to see the lights blink faster, but I didn’t want to edit it in both places every time. So I defined the variable in line 2, and the delays reference that variable rather than a hard coded integer. It’s a small change that lets me edit the delays on the fly.
Here’s my final code:
int switchState = 0; int delayTime = 100; void setup (){ pinMode (3, OUTPUT); // green LED pinMode (4, OUTPUT); // red LED pinMode (5, OUTPUT); // red LED pinMode (2, INPUT); // switch } void loop(){ switchState = digitalRead(2); // check voltage on pin 2 (switch) if (switchState == LOW) { // if the button is not pressed digitalWrite (3, HIGH); // turn on green LED digitalWrite (4, LOW); // turn off red LED digitalWrite (5, LOW); // turn off red LED } else { // if the button is pressed digitalWrite (3, LOW); // turn off green LED digitalWrite (4, LOW); // turn off first red LED digitalWrite (5, HIGH); // turn on second red LED delay(delayTime); // wait // toggle the LEDs digitalWrite (4, HIGH); // turn on first red LED digitalWrite (5, LOW); // turn off second red LED delay(delayTime); // wait } }
I would give myself an A for this project because I successfully completed the tutorial and made an original customization to the code.
One reply on “Spaceship Interface”
[…] the code–unlike last week, I followed the exact instructions in the project […]