Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
189 views10 pages

Arduino Stoplight Code

The document contains code for an Arduino project that controls LED lights using shift registers. It defines arrays to store the data patterns for different color sequences to display on the LEDs. It initializes the pin connections for the shift registers and buttons. It sets the initial data patterns for a red, yellow, and green light sequence.

Uploaded by

davesd7128
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
189 views10 pages

Arduino Stoplight Code

The document contains code for an Arduino project that controls LED lights using shift registers. It defines arrays to store the data patterns for different color sequences to display on the LEDs. It initializes the pin connections for the shift registers and buttons. It sets the initial data patterns for a red, yellow, and green light sequence.

Uploaded by

davesd7128
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <AF_Wave.

h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"

#define DEBOUNCE 20

#define swPin 14

AF_Wave card;
File f;
Wavefile wave;

int stopPlayingButton = 15;


int playButton = 16;

//Pin connected to ST_CP of 74HC595


int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 9;
////Pin connected to DS of 74HC595
int dataPin = 6;

//holders for infromation you're going to pass to shifting function


//byte dataRED;
//byte dataGREEN;
//byte dataYELLOW;
//byte dataRED2;
//byte dataGREEN2;
//byte dataYELLOW2;
byte dataArrayRED[10];
byte dataArrayGREEN[10];
byte dataArrayYELLOW[10];
byte dataArrayRED2[10];
byte dataArrayGREEN2[10];
byte dataArrayYELLOW2[10];

byte s1dataArrayRED[10];
byte s1dataArrayGREEN[10];
byte s1dataArrayYELLOW[10];
byte s1dataArrayRED2[10];
byte s1dataArrayGREEN2[10];
byte s1dataArrayYELLOW2[10];

byte s2dataArrayRED[8];
byte s2dataArrayGREEN[8];
byte s2dataArrayYELLOW[8];
byte s2dataArrayRED2[8];
byte s2dataArrayGREEN2[8];
byte s2dataArrayYELLOW2[8];

int delayArray[] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 500, 500};
int s1delayArray[] = {425, 425, 425, 425, 425, 425, 425, 425, 425, 425};
int s2delayArray[] = {475, 475, 475, 530, 475, 475, 475, 530};
int sequence = 0;

int shiftindex = 0;
int cursong = 2;

void setup() {
byte i;
pinMode(latchPin, OUTPUT);
pinMode(stopPlayingButton, INPUT);
pinMode(playButton, INPUT);

dataArrayRED[0] = 0xFF; //11111111


dataArrayRED[1] = 0xFF; //11111110
dataArrayRED[2] = 0xFF; //11111100
dataArrayRED[3] = 0xFF; //11111000
dataArrayRED[4] = 0x00; //11110000
dataArrayRED[5] = 0x00; //11100000
dataArrayRED[6] = 0x00; //11000000
dataArrayRED[7] = 0x00; //10000000
dataArrayRED[8] = 0x00; //00000000
dataArrayRED[9] = 0x00; //11100000

dataArrayRED2[0] = 0xF0; //11111111


dataArrayRED2[1] = 0xF0; //11111110
dataArrayRED2[2] = 0xF0; //11111100
dataArrayRED2[3] = 0xF0; //11111000
dataArrayRED2[4] = 0x00; //11110000
dataArrayRED2[5] = 0x00; //11100000
dataArrayRED2[6] = 0x00; //11000000
dataArrayRED2[7] = 0x00; //10000000
dataArrayRED2[8] = 0x0F; //00000000
dataArrayRED2[9] = 0x0F; //11100000

dataArrayYELLOW[0] = 0x00; //11111111


dataArrayYELLOW[1] = 0x00; //11111110
dataArrayYELLOW[2] = 0x00; //11111100
dataArrayYELLOW[3] = 0x00; //11111000
dataArrayYELLOW[4] = 0x00; //11110000
dataArrayYELLOW[5] = 0x00; //11100000
dataArrayYELLOW[6] = 0x00; //11000000
dataArrayYELLOW[7] = 0x00; //10000000
dataArrayYELLOW[8] = 0xFF; //00000000
dataArrayYELLOW[9] = 0xFF; //11100000

dataArrayYELLOW2[0] = 0x00; //11111111


dataArrayYELLOW2[1] = 0x00; //11111110
dataArrayYELLOW2[2] = 0x00; //11111100
dataArrayYELLOW2[3] = 0x00; //11111000
dataArrayYELLOW2[4] = 0xFF; //11110000
dataArrayYELLOW2[5] = 0xFF; //11100000
dataArrayYELLOW2[6] = 0xFF; //11000000
dataArrayYELLOW2[7] = 0xFF; //10000000
dataArrayYELLOW2[8] = 0x00; //00000000
dataArrayYELLOW2[9] = 0x00; //11100000

dataArrayGREEN[0] = 0x00; //11111111


dataArrayGREEN[1] = 0x00; //01111111
dataArrayGREEN[2] = 0x00; //00111111
dataArrayGREEN[3] = 0x00; //00011111
dataArrayGREEN[4] = 0xF0; //00001111
dataArrayGREEN[5] = 0xF0; //00000111
dataArrayGREEN[6] = 0xF0; //00000011
dataArrayGREEN[7] = 0xF0; //00000001
dataArrayGREEN[8] = 0x00; //00000000
dataArrayGREEN[9] = 0x00; //00000111

dataArrayGREEN2[0] = 0x00; //11111111


dataArrayGREEN2[1] = 0x00; //01111111
dataArrayGREEN2[2] = 0x00; //00111111
dataArrayGREEN2[3] = 0x00; //00011111
dataArrayGREEN2[4] = 0x00; //00001111
dataArrayGREEN2[5] = 0x00; //00000111
dataArrayGREEN2[6] = 0x00; //00000011
dataArrayGREEN2[7] = 0x00; //00000001
dataArrayGREEN2[8] = 0x00; //00000000
dataArrayGREEN2[9] = 0x00; //00000111

setMusicSeqVals();

// set up serial port


Serial.begin(9600);

// set up waveshield pins


for (byte i = 2; i <= 5; ++i) {
pinMode(i, OUTPUT);
}

// enable pull-up resistors on


// switch pins (analog inputs)
for (byte i = swPin; i <= swPin + 5; ++i) {
digitalWrite(i, HIGH);
}

// open memory card


if (!card.init_card()) {
putstring_nl("Card init failed!"); return;
}
if (!card.open_partition()) {
putstring_nl("No partition!"); return;
}
if (!card.open_filesys()) {
putstring_nl("Couldn't open filesys"); return;
}
if (!card.open_rootdir()) {
putstring_nl("Couldn't open dir"); return;
}
}

void setMusicSeqVals()
{
//if (sequence == 1)
//{
s1dataArrayRED[0] = 0x1B; //11111111
s1dataArrayRED[1] = 0x1B; //11111110
s1dataArrayRED[2] = 0x00; //11111100
s1dataArrayRED[3] = 0x1B; //11111000
s1dataArrayRED[4] = 0x1B; //11110000
s1dataArrayRED[5] = 0x1B; //11100000
s1dataArrayRED[6] = 0x1B; //11000000
s1dataArrayRED[7] = 0x00; //10000000
s1dataArrayRED[8] = 0xAB; //00000000
s1dataArrayRED[9] = 0xDD; //11100000

s1dataArrayRED2[0] = 0x60; //11111111


s1dataArrayRED2[1] = 0x60; //11111110
s1dataArrayRED2[2] = 0x00; //11111100
s1dataArrayRED2[3] = 0x60; //11111000
s1dataArrayRED2[4] = 0x60; //11110000
s1dataArrayRED2[5] = 0x60; //11100000
s1dataArrayRED2[6] = 0x60; //11000000
s1dataArrayRED2[7] = 0x00; //10000000
s1dataArrayRED2[8] = 0xEE; //00000000
s1dataArrayRED2[9] = 0x35; //11100000

s1dataArrayYELLOW[0] = 0xDE; //11111111


s1dataArrayYELLOW[1] = 0xBE; //11111110
s1dataArrayYELLOW[2] = 0xAF; //11111100
s1dataArrayYELLOW[3] = 0x3F; //11111000
s1dataArrayYELLOW[4] = 0x2B; //11110000
s1dataArrayYELLOW[5] = 0xCD; //11100000
s1dataArrayYELLOW[6] = 0xF4; //11000000
s1dataArrayYELLOW[7] = 0x47; //10000000
s1dataArrayYELLOW[8] = 0x89; //00000000
s1dataArrayYELLOW[9] = 0x99; //11100000

s1dataArrayYELLOW2[0] = 0xDD; //11111111


s1dataArrayYELLOW2[1] = 0xEE; //11111110
s1dataArrayYELLOW2[2] = 0xAA; //11111100
s1dataArrayYELLOW2[3] = 0x44; //11111000
s1dataArrayYELLOW2[4] = 0x55; //11110000
s1dataArrayYELLOW2[5] = 0x66; //11100000
s1dataArrayYELLOW2[6] = 0x77; //11000000
s1dataArrayYELLOW2[7] = 0x88; //10000000
s1dataArrayYELLOW2[8] = 0x99; //00000000
s1dataArrayYELLOW2[9] = 0xEE; //11100000

s1dataArrayGREEN[0] = 0xAA; //11111111


s1dataArrayGREEN[1] = 0x46; //01111111
s1dataArrayGREEN[2] = 0x78; //00111111
s1dataArrayGREEN[3] = 0x12; //00011111
s1dataArrayGREEN[4] = 0x35; //00001111
s1dataArrayGREEN[5] = 0x57; //00000111
s1dataArrayGREEN[6] = 0x98; //00000011
s1dataArrayGREEN[7] = 0x56; //00000001
s1dataArrayGREEN[8] = 0x23; //00000000
s1dataArrayGREEN[9] = 0x15; //00000111

s1dataArrayGREEN2[0] = 0x00; //11111111


s1dataArrayGREEN2[1] = 0x00; //01111111
s1dataArrayGREEN2[2] = 0x00; //00111111
s1dataArrayGREEN2[3] = 0x00; //00011111
s1dataArrayGREEN2[4] = 0x00; //00001111
s1dataArrayGREEN2[5] = 0x00; //00000111
s1dataArrayGREEN2[6] = 0x00; //00000011
s1dataArrayGREEN2[7] = 0x00; //00000001
s1dataArrayGREEN2[8] = 0x00; //00000000
s1dataArrayGREEN2[9] = 0x00; //00000111

s2dataArrayRED[0] = 0xAB; //11111111


s2dataArrayRED[1] = 0x1D; //11111110
s2dataArrayRED[2] = 0xDD; //11111100
s2dataArrayRED[3] = 0xFF; //11111000
s2dataArrayRED[4] = 0x67; //11110000
s2dataArrayRED[5] = 0xAB; //11100000
s2dataArrayRED[6] = 0xCD; //11000000
s2dataArrayRED[7] = 0xFF; //10000000

s2dataArrayRED2[0] = 0xCF; //11111111


s2dataArrayRED2[1] = 0x55; //11111110
s2dataArrayRED2[2] = 0xAD; //11111100
s2dataArrayRED2[3] = 0xFF; //11111000
s2dataArrayRED2[4] = 0x67; //11110000
s2dataArrayRED2[5] = 0x8F; //11100000
s2dataArrayRED2[6] = 0xD3; //11000000
s2dataArrayRED2[7] = 0xFF; //10000000

s2dataArrayYELLOW[0] = 0xDE; //11111111


s2dataArrayYELLOW[1] = 0xBE; //11111110
s2dataArrayYELLOW[2] = 0xAF; //11111100
s2dataArrayYELLOW[3] = 0xFF; //11111000
s2dataArrayYELLOW[4] = 0x2B; //11110000
s2dataArrayYELLOW[5] = 0xCD; //11100000
s2dataArrayYELLOW[6] = 0xF4; //11000000
s2dataArrayYELLOW[7] = 0xFF; //10000000

s2dataArrayYELLOW2[0] = 0xDD; //11111111


s2dataArrayYELLOW2[1] = 0xEE; //11111110
s2dataArrayYELLOW2[2] = 0xAA; //11111100
s2dataArrayYELLOW2[3] = 0xFF; //11111000
s2dataArrayYELLOW2[4] = 0x55; //11110000
s2dataArrayYELLOW2[5] = 0x66; //11100000
s2dataArrayYELLOW2[6] = 0x77; //11000000
s2dataArrayYELLOW2[7] = 0xFF; //10000000

s2dataArrayGREEN[0] = 0xAA; //11111111


s2dataArrayGREEN[1] = 0x46; //01111111
s2dataArrayGREEN[2] = 0x78; //00111111
s2dataArrayGREEN[3] = 0xFF; //00011111
s2dataArrayGREEN[4] = 0x35; //00001111
s2dataArrayGREEN[5] = 0x57; //00000111
s2dataArrayGREEN[6] = 0x98; //00000011
s2dataArrayGREEN[7] = 0xFF; //00000001

s2dataArrayGREEN2[0] = 0x00; //11111111


s2dataArrayGREEN2[1] = 0x00; //01111111
s2dataArrayGREEN2[2] = 0x00; //00111111
s2dataArrayGREEN2[3] = 0xFF; //00011111
s2dataArrayGREEN2[4] = 0x00; //00001111
s2dataArrayGREEN2[5] = 0x00; //00000111
s2dataArrayGREEN2[6] = 0x00; //00000011
s2dataArrayGREEN2[7] = 0xFF; //00000001

//}
}

void loop() {
if (wave.isplaying)
{
lights();
}
else
{
sequence = 0;
lights();
}
switch (check_switches()) {
case 1:
putstring_nl("1");
sequence = 0;
playfile("SILENT.WAV");
break;
case 2:
putstring_nl("HLLWN");
sequence = 1;
shiftindex = 0;
playfile("HLLWN.WAV");
break;
case 3:
putstring_nl("3");
sequence = 2;
shiftindex = 0;
playfile("THRILLER.WAV");
// break;
//case 4:
// putstring_nl("4");
// sequence = 3;
// playfile("CAT.WAV");
}
}

byte check_switches()
{
static byte previous;
static long time;
static byte previous2;
static long time2;
byte reading;
byte reading2;
byte pressed;
byte index;

pressed = 0;
//for (index = 0; index < 6; ++index) {
reading = digitalRead(stopPlayingButton);
reading2 = digitalRead(playButton);
if(reading == HIGH)
{
//Serial.println(index);
//Serial.println("HIGH");
}
else
{
//Serial.println(index);
//Serial.println("LOW");
}
if (reading == HIGH && previous == LOW && millis() - time > DEBOUNCE)
{
// switch pressed
time = millis();
pressed = 1;
//break;
}
else if(reading2 == HIGH && previous2 == LOW && millis() - time2 > DEBOUNCE)
{
time2 = millis();

if (cursong > 3)
{
cursong = 2;
}
pressed = cursong;
cursong = cursong + 1;
}

previous = reading;
previous2 = reading2;
//}
// return switch number (1 - 6)
return (pressed);
}

void playfile(char *name) {


// stop any file already playing
if (wave.isplaying) {
wave.stop();
}
// close file if open
if (f) {
card.close_file(f);
}
// play specified file
f = card.open_file(name);
if (f && wave.create(f)) {
wave.play();
}
}

void lights() {
//dataRED = dataArrayRED[shiftindex];
//dataGREEN = dataArrayGREEN[shiftindex];
digitalWrite(latchPin, 0);
Serial.println(shiftindex);
if(sequence == 0)
{
//move 'em out
shiftOut(dataPin, clockPin, dataArrayRED[shiftindex]);
shiftOut(dataPin, clockPin, dataArrayRED2[shiftindex]);
shiftOut(dataPin, clockPin, dataArrayYELLOW[shiftindex]);
shiftOut(dataPin, clockPin, dataArrayYELLOW2[shiftindex]);
shiftOut(dataPin, clockPin, dataArrayGREEN[shiftindex]);
shiftOut(dataPin, clockPin, dataArrayGREEN2[shiftindex]);
//return the latch pin high to signal chip that it
//no longer needs to listen for information

delay(delayArray[shiftindex]);

shiftindex = shiftindex + 1;
if (shiftindex >= 10 )
{
shiftindex = 0;
}
}
else if(sequence == 1)
{
//move 'em out
shiftOut(dataPin, clockPin, s1dataArrayRED[shiftindex]);
shiftOut(dataPin, clockPin, s1dataArrayRED2[shiftindex]);
shiftOut(dataPin, clockPin, s1dataArrayYELLOW[shiftindex]);
shiftOut(dataPin, clockPin, s1dataArrayYELLOW2[shiftindex]);
shiftOut(dataPin, clockPin, s1dataArrayGREEN[shiftindex]);
shiftOut(dataPin, clockPin, s1dataArrayGREEN2[shiftindex]);
//return the latch pin high to signal chip that it
//no longer needs to listen for information

delay(s1delayArray[shiftindex]);

shiftindex = shiftindex + 1;
if (shiftindex >= 10 )
{
shiftindex = 0;
}
}
else if(sequence == 2)
{
//move 'em out
shiftOut(dataPin, clockPin, s2dataArrayRED[shiftindex]);
shiftOut(dataPin, clockPin, s2dataArrayRED2[shiftindex]);
shiftOut(dataPin, clockPin, s2dataArrayYELLOW[shiftindex]);
shiftOut(dataPin, clockPin, s2dataArrayYELLOW2[shiftindex]);
shiftOut(dataPin, clockPin, s2dataArrayGREEN[shiftindex]);
shiftOut(dataPin, clockPin, s2dataArrayGREEN2[shiftindex]);
//return the latch pin high to signal chip that it
//no longer needs to listen for information

delay(s2delayArray[shiftindex]);

shiftindex = shiftindex + 1;
if (shiftindex >= 8 )
{
shiftindex = 0;
}
}

digitalWrite(latchPin, 1);

// the heart of the program


void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low

//internal function setup


int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);

//clear everything out just in case to


//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);

//for each bit in the byte myDataOut?


//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);

//if the value passed to myDataOut and a bitmask result


// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}

//Sets the pin to HIGH or LOW depending on pinState


digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}

//stop shifting
digitalWrite(myClockPin, 0);
}

//blinks the whole register based on the number of times you want to
//blink "n" and the pause between them "d"
//starts with a moment of darkness to make sure the first blink
//has its full visual effect.
void blinkAll_2Bytes(int n, int d) {
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
digitalWrite(latchPin, 1);
delay(200);
for (int x = 0; x < n; x++) {
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 255);
shiftOut(dataPin, clockPin, 255);
shiftOut(dataPin, clockPin, 255);
shiftOut(dataPin, clockPin, 255);
shiftOut(dataPin, clockPin, 255);
shiftOut(dataPin, clockPin, 255);
digitalWrite(latchPin, 1);
delay(d);
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
digitalWrite(latchPin, 1);
delay(d);
}
}

You might also like