Professional Documents
Culture Documents
Arduino Stoplight Code
Arduino Stoplight Code
h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
#define DEBOUNCE 20
#define swPin 14
AF_Wave card;
File f;
Wavefile wave;
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);
setMusicSeqVals();
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
//}
}
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 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);
//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);
}
}