Scalextric Race Lights – Part 1

Project 1

Scalextric Start Lights

By James Winters

Aim

The aim of my first project was to produce some start lights to go with my Scalextric which you would be able to press a button and the lights would count down to the start. I was required to produce a final design which consisted of at least one input and two outputs, or the other way round.

The final design I came up with consisted of two buttons, five LED lights and a Piezo Speaker with each button deciding on a different scenario.

How It Works

If you press Button 1…

  1. All 5 LED lights would flash would a pre-set defined on time and off time, to simulate the warning of the start of the race to the competitors

If you press Button 2…

  1. All 5 LED lights would light up and then would begin to go out over a period of 7 seconds (delay of 1.5 seconds) between each one going out
  2. Once all the lights had gone out the Piezo Speaker would play a tune. I wanted the theme tune from BBC’s Formula One introduction.

Electrical Components Required

All the electrical components which I used came from the starter kit offered by Oomlout

http://oomlout.co.uk/collections/arduino/products/arduino-starter-kit-ardx

The electrical components from the kit I required to produce my set up were:

  • 5 x Red LED’s
  • 2 x Push Buttons
  • 5 x 560 ohm resistors (green, blue, brown)
  • Piezo speaker
  • A selection of Jumper Wires (red for positive and black for ground)
  • 9v battery and Arduino 9v power adapter (in order for operation of the Arduino away from a USB connection)

P1 ARDUINO SETUP

Initial Testing – Electrical

When producing my design I didn’t throw myself in at the deep end and get everything to work as one complete system. This can cause problems in debugging a problem. Also best practice that I used to make things easier was to use colour coordination with my wiring. Generally I used red for to the components and then black for away from the component (ground)..

I split the components into separate components to begin with making the workload easier. This applies to the wiring of the breadboard and the Arduino code. I built up the code by means of:

  1. Getting one LED to flash
  2. Adding an additional four LED’s to make them flash in the same fashion
  3. Add Button 1 into the circuit and make the LED’s flash once the button is pressed, otherwise they would stay out. By means of a “if” and “else” statement
  4. Add Button 2 into the circuit and add another condition if this button was pressed (a new “if” and “else” statement). This wouldn’t require a complete new set of code and would just require copying and modifying of Button 1’s code. No need to start from scratch.
  5. Add the Piezo Speaker to the circuit. The Piezo Speaker is activated by the Button 2 so requires to be added to this if/else statement to work at the right point. I required to find the tones/speaker values as well as the notes to the BBC Formula One theme tune!

Getting The LED’s To Flash (Blink)

To get the LED’s to flash just required the basic example code from the Arduino program itself. Defining which digital pin the LED was going to be controlled by the Arduino, defining the LED as an output and then writing the state of the LED in the loop as well as delays

Code

int ledPin0 = 3; //Putting LED pin 0 to Digital pin 3

int ledPin1 = 4; Putting LED pin 1 to Digital pin 4

int ledPin2 = 5; Putting LED pin 2 to Digital pin 5

int ledPin3 = 6; Putting LED pin 3 to Digital pin 6

int ledPin4 = 7; Putting LED pin 4 to Digital pin 7

void setup()

{

pinMode(ledPin0, OUTPUT);

pinMode(ledPin1, OUTPUT);

pinMode(ledPin2, OUTPUT);

pinMode(ledPin3, OUTPUT);

pinMode(ledPin4, OUTPUT);

}

void loop() {

digitalWrite(ledPin0, HIGH);

digitalWrite(ledPin1, HIGH);

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin3, HIGH);

digitalWrite(ledPin4, HIGH);

delay(500);

digitalWrite(ledPin0, LOW);

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, LOW);

delay(500);

digitalWrite(ledPin0, HIGH);

digitalWrite(ledPin1, HIGH);

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin3, HIGH);

digitalWrite(ledPin4, HIGH);

}

This very basic code allowed the five LED’s to flash on and off. Defining the LED’s to their given Arduino pins, defining them as outputs and then writing the loop for the task for the Arduino to take. In this case for all the LED’s to come on, stay on for ½ a second, then go off for ½ a second and then come on again.

Adding Button 1 In (Button code)

Adding the button into the code required an “If” state and a digital read to read the state of what the button was in. Whether it was high (pushed down for on) or low (off).   If it was pushed down I wanted this to initiate the flashing of the system

This button code was found as part of the Arduino example code and required tweaking for the output I wanted.

Additions To The Code (These were placed in an addition to the code given above)

int buttonApin = 11;

void setup() {

pinMode(buttonApin, INPUT);

Serial.begin(9600);

}

void loop() {

Serial.print(digitalRead(buttonApin));

if (digitalRead(buttonApin) == HIGH)

{

Serial.println (“Warning Initiated”);//add before (digitalWrite…)

}

If the Button A wasn’t pushed (i.e. remained low), nothing would happen to the system. The Serial.println part of the code is designed in order for the Serial Monitor button in the top right hand corner to be used to see if the code is doing as it should be correctly.

Adding Button 2

The addition of Button 2 and what I wanted to do with it required the same method as adding in the first button, but as I wanted the Piezo Speaker to want play a tune after the lights all went out I required extra code in which to read the tones, which Arduino pin the speaker was connected to and the number of tones required to be played.

Additions To The Code To Add Button 2

int buttonBpin = 10;

int speakerOUT = 12;

int numTones = 10;

int tones[] = {587,587,659,698,659,587,523,587,659,440};

//            mid C  C#   D    D#   E    F    F#   G    G#   A

void setup() {

pinMode(speakerOUT,OUTPUT);

}

Then to be added after the “if” statement for Button 1

void loop(){

if (digitalRead(buttonBpin) == HIGH)

{

Serial.println (“Race Start Beginning”);

{

//Digital.Write…

}

for (int i = 0; i < numTones; i++)

{

tone(speakerOUT, tones[i]);

delay(500);//plays each note for ½ second

noTone(speakerOUT);//makes sure Piezo Speaker switches off after playing tune

}

The reference to this code is at https://learn.adafruit.com/adafruit-arduino-lesson-10-making-sounds/playing-a-scale

I found Adafruit very useful and easy to understand especially seeing I was new to the concept of Arduino, coding and electronics in general.

When I got the code working when pressing Button 2 I found it would result in all the lights go out as they should and then playing the tune, but the last tone of the tune would stay on and not go off until I pull the power on the Arduino! Therefore I required a “noTone” addition to clearly state that I wanted the tune to stop playing once the final tone had been played. Silence!

For the Arduino to determine the tone to play requires a physical number to be chosen. I found this source which clearly states which numbers assign to which note of music. https://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation

If I wanted the Arduino to play more notes increase the value of the int numTones and add another number to the sequence on the int Tones definition. To play the BBC Formula One theme tune I just needed to find the musical notes (easily found by piano demonstrations on YouTube), or just look for how to play The Chain by Fleetwood Mac!

The “for” statement was used for the Arduino to read the notes I gave to it. When ‘i’ is 0 it reads 587, then adds one (i++) to read 587 again and it continues to repeat until there’s no tones to play. That would be the reason why without the “noTone” added that the code will continue to play.

P1 FRITZING

The Final Code Combined (simply place into Arduino software)

int ledPin0 = 3;

int ledPin1 = 4;

int ledPin2 = 5;

int ledPin3 = 6;

int ledPin4 = 7;

int buttonApin = 11;

int buttonBpin = 10;

int speakerOUT = 12;

int numTones = 10;

int tones[] = {587,587,659,698,659,587,523,587,659,440};

//            mid C  C#   D    D#   E    F    F#   G    G#   A

void setup()

{

pinMode(ledPin0, OUTPUT);

pinMode(ledPin1, OUTPUT);

pinMode(ledPin2, OUTPUT);

pinMode(ledPin3, OUTPUT);

pinMode(ledPin4, OUTPUT);

pinMode(buttonApin, INPUT);

pinMode(buttonBpin, INPUT);

pinMode(speakerOUT,OUTPUT);

Serial.begin(9600);

}

void loop()

{

Serial.print(digitalRead(buttonApin));

if (digitalRead(buttonApin) == HIGH)

{

Serial.println (“Warning Initiated”);

digitalWrite(ledPin0, HIGH);

digitalWrite(ledPin1, HIGH);

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin3, HIGH);

digitalWrite(ledPin4, HIGH);

delay(500);

digitalWrite(ledPin0, LOW);

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, LOW);

delay(500);

digitalWrite(ledPin0, HIGH);

digitalWrite(ledPin1, HIGH);

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin3, HIGH);

digitalWrite(ledPin4, HIGH);

delay(500);

digitalWrite(ledPin0, LOW);

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, LOW);

delay(500);

digitalWrite(ledPin0, HIGH);

digitalWrite(ledPin1, HIGH);

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin3, HIGH);

digitalWrite(ledPin4, HIGH);

delay(500);

digitalWrite(ledPin0, LOW);

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, LOW);

delay(500);

digitalWrite(ledPin0, HIGH);

digitalWrite(ledPin1, HIGH);

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin3, HIGH);

digitalWrite(ledPin4, HIGH);

delay(500);

digitalWrite(ledPin0, LOW);

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, LOW);

delay(500);

digitalWrite(ledPin0, HIGH);

digitalWrite(ledPin1, HIGH);

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin3, HIGH);

digitalWrite(ledPin4, HIGH);

delay(500);

digitalWrite(ledPin0, LOW);

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, LOW);

delay(500);

digitalWrite(ledPin0, HIGH);

digitalWrite(ledPin1, HIGH);

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin3, HIGH);

digitalWrite(ledPin4, HIGH);

}

if (digitalRead(buttonBpin) == HIGH)

{

Serial.println (“Race Start Beginning”);

digitalWrite(ledPin0, HIGH);

delay(1500);

digitalWrite(ledPin1, HIGH);

delay(1500);

digitalWrite(ledPin2, HIGH);

delay(1500);

digitalWrite(ledPin3, HIGH);

delay(1500);

digitalWrite(ledPin4, HIGH);

delay(3000);

digitalWrite(ledPin0, LOW);

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, LOW);

}

for (int i = 0; i < numTones; i++)

{

tone(speakerOUT, tones[i]);

delay(500);

noTone(speakerOUT);

}

}

P1 SETUP

Producing The Final Model

The final design model in which to go with the Scalextric was produced from Foamcore in the shape of a real equivalent starting bridge, with scaling to the cars and track; the basic stuff. I cut the design into three allowing for two outside solid parts which would outer facing sides and then the centre piece which I could create a small wall thickness and then get rid of the rest in order to allow for the LED’s and the wiring to fit inside tidily. One side was fully cut out to allow the wiring to get to the Arduino. Wires would go to and from the LED’s to allow for them to be mounted into the starting bridge. Covered with paper by double siding to make it fit the surroundings of the Formula One scene!

P1 SETUP 2 P1 SETUP 3

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s