Arduino Starter Series - The Spaceship Hyperdrive

This is a project from my Arduino starter series.

In this project I'm going to make a spaceship hyperdrive, well not really but this project is from the Arduino Projects Book and it's an awesome learning experience on the way to Arduino mastery.

My code for this example greatly differs from the example provided in the Arduino Projects Book, I decided to use the Bounce2 library to set the button up easier. It makes life easier when dealing with push buttons.

This project is ideal for beginners who want to grasp the basics of Arduino programming and prototyping. Enjoy!

Project Requirements

Arduino Uno/Duemilanove/Diecimila
USB cable to program Arduino
Breadboard
Three LEDS (Two red and one green)
Three 220 Ohm Resistors
Two 10k Resistors
One Pushbutton
Wires to power the components on the breadboard

The Breadboard Setup

Ensure that your configuration is the same as specified in this image

Arduino Sketch

As usual, I've commented the code as mush as possible to explain every line.

#include <Bounce2.h>

// These are the pins for the red and green LEDS that tell us the hyperdrives state
const int redPin1 = 5;  
const int redPin2 = 6;  
const int greenPin = 4;

// Set the pin for the button so we can tell the hyperdrive what to do
const int buttonPin = 3;  
const int bounceDelay = 5;

// Using the Bounce 2 Library to create a button with debounce functionality
// Read more here:
// https://github.com/thomasfredericks/Bounce2
Bounce hyperButton = Bounce(); // The button will fire up our hyperdrive

void setup() {  
  // LED setup
  pinMode(redPin1, OUTPUT);
  pinMode(redPin2, OUTPUT);
  pinMode(greenPin, OUTPUT);

  // Hyper drive button setup
  hyperButton.attach(buttonPin);
  hyperButton.interval(bounceDelay);
}

// The loop function
void loop() {  
  hyperButton.update(); // The value of the Bounce2 button needs to be updated once per loop before reading the value
  int value = hyperButton.read(); // We read the value of the button and pass it to a int called value
  if (value == HIGH) {
    hyperLaunch(); // If the value is HIGH then the hyperdrive is engaged
  } else {
    hyperStandby(); // If the value is LOW then the hyperdrive is in standby
  }
}

// Hyper drive standby function
void hyperStandby() {  
  digitalWrite(redPin1, LOW);
  digitalWrite(redPin2, LOW);
  digitalWrite(greenPin, HIGH); // When in standby mode the green LED is on
}

// Hyperdrive engaged functionality
// Flash the red LEDs in sequence to indicate hyperdrive is engaged
void hyperLaunch() {  
  digitalWrite(greenPin, LOW); // The green LED must be disabled here
  digitalWrite(redPin1, HIGH);
  digitalWrite(redPin2, LOW);
  delay(100);
  digitalWrite(redPin1, LOW);
  digitalWrite(redPin2, HIGH);
  delay(100);
}

This project is available on my Github page

Comments

comments powered by Disqus