Thanks mate,
Looking at your code was dreading how you put it together.
Link to image forum, sorry will not let me up upload currently.
http://forums.adafruit.com/viewtopic.php?f=25&t=38620
I currently have the following code and am wondering if you could take a look at it, I had 4 shift registers daisy chained together and need it to do the following:
When the user presses button 0 once the first sequence is carried out
when the user presses button 0 twice the second sequence is carried out
When the user presses button 1 the second sequence is carried out
when the user presses button 1 twice the first sequence is carried out
when the first set of LED's are lit, meaning both tubes they need to turn off to allow the second lot to follow suit lighting the same colours as the first tubes, as bit confusing I know but to help I uploaded a picture to show the project.
Anyway once they have lit up they need to light the final set which will be based upon pin 10. to complete the cycle.
Code: Select all
#include <Button.h>
int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int outputEnablePin = 3;
int ledPin = 10;
//Button trigger = Button(2,LOW);
int sr1Colour = 0; // Set for ShiftReg1 as 0 = off, 1 = green, 2 = red
int sr2Colour = 0;
byte leds = 0;
byte button[] = {8, 9};
byte switch_value = 0;
boolean btnOn = false;
int count = 0;
#define NUMBUTTONS sizeof(button)
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
void setup() {
setBrightness(0);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(outputEnablePin, OUTPUT);
//pinMode(2,INPUT); //Trigger switch sits on pin 2
Serial.begin(9600);
Serial.println("Welcome");
byte i;
for (i=0; i< NUMBUTTONS; i++) {
pinMode(button[i], INPUT);
digitalWrite(button[i], HIGH);
}
//Timer2 Overflow Interrupt Enable
TIMSK2 |= 1<<TOIE2;
}
//byte pressCount1 = 0;
SIGNAL(TIMER2_OVF_vect) {
check_switches();
}
void check_switches()
{
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
byte index;
for (index = 0; index < NUMBUTTONS; index++) {
currentstate[index] = digitalRead(button[index]); // read the button
/*
Serial.print(index, DEC);
Serial.print(": cstate=");
Serial.print(currentstate[index], DEC);
Serial.print(", pstate=");
Serial.print(previousstate[index], DEC);
Serial.print(", press=");
*/
if (currentstate[index] == previousstate[index]) {
if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
// just pressed
justpressed[index] = 1;
count = count+1;
//switch_value = switch_value + 1;
}
else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
// just released
justreleased[index] = 1;
}
pressed[index] = !currentstate[index]; // remember, digital HIGH means NOT pressed
}
//Serial.println(pressed[index], DEC);
previousstate[index] = currentstate[index]; // keep a running tally of the buttons
}
}
void loop() {
/////////////////////////////////////////////////////
//This is Button 1 Set Press
/////////////////////////////////////////////////////
if(justpressed[0] && btnOn)
{
count;
sequence2();
justpressed[0] = 0;
btnOn = false;
}
if(justpressed[0]) {
Serial.println("Button 0 pressed");
btnOn = true;
count;
justpressed[0] = 0;
sequence1();
}
// /////////////////////////////////////////////////////
// //This is Button 2 Set Press
// /////////////////////////////////////////////////////
if(justpressed[1] && btnOn)
{
count;
sequence1();
justpressed[1] = 0;
btnOn = false;
}
if(justpressed[1]) {
Serial.println("Button 1 pressed");
btnOn = true;
count;
justpressed[1] = 0;
sequence2();
}
// /////////////////////////////////////////////////////
// //Run final Sequence1
// /////////////////////////////////////////////////////
if (sr1Colour ==1 && sr2Colour==1){
sequenceFinal1();
ledPin = HIGH;
}
// /////////////////////////////////////////////////////
// //Run final Sequence2
// /////////////////////////////////////////////////////
if (sr1Colour ==1 && sr2Colour==2){
sequenceFinal2();
ledPin = HIGH;
}
// /////////////////////////////////////////////////////
// //Run final Sequence3
// /////////////////////////////////////////////////////
if (sr1Colour ==1 && sr2Colour==1){
sequenceFinal3();
ledPin = HIGH;
}
// /////////////////////////////////////////////////////
// //Run final Sequence4
// /////////////////////////////////////////////////////
if (sr1Colour ==1 && sr2Colour==2){
sequenceFinal4();
ledPin = HIGH;
}
}
/////////////////////////////////////////////////////
//This is updateShiftRegister
////////////////////////////////////////////////////
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 1
digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
//This is updateShiftRegister2
////////////////////////////////////////////////////
void updateShiftRegister2()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
//This is updateShiftRegister3
////////////////////////////////////////////////////
void updateShiftRegister3()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 1
digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
//This is updateShiftRegister4
////////////////////////////////////////////////////
void updateShiftRegister4()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
//This is updateShiftRegister5
////////////////////////////////////////////////////
void updateShiftRegister5()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 8
digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
//This is updateShiftRegister6
////////////////////////////////////////////////////
void updateShiftRegister6()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
//This is updateShiftRegister7
////////////////////////////////////////////////////
void updateShiftRegister7()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 8
digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
//This is updateShiftRegister8
////////////////////////////////////////////////////
void updateShiftRegister8()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
//This is setBrightness
////////////////////////////////////////////////////
void setBrightness(byte brightness) // 0 to 255
{
analogWrite(outputEnablePin, 255-brightness);
}
/////////////////////////////////////////////////////
//This is Sequence 1
////////////////////////////////////////////////////
void sequence1(){
Serial.println("button 1");
for (int i = 0; i < 4; i++)
{
bitSet(leds, i);
updateShiftRegister();
delay(50);
for (byte b = 0; b < 255; b++)
{
setBrightness(b);
delay(20);
}
Serial.println(count);
if(count>=3){
Serial.println("Count =");
Serial.print(count);
leds = 0;
//for (int i = 0; i < 4; i++)
{
bitSet(leds, -5);
updateShiftRegister();
delay(50);
setBrightness(255);
}
count = 0;
break;
}
if (justpressed[0] && btnOn && (count >=3)) {
Serial.println("Button 0 pressed again");
count;
leds = 0;
setBrightness(255);
break;
}
}
//bitSet(leds,0);
//delay(1000);
sr1Colour = 1; //Set SR1 to green
leds = 0;
//switch_value = 0;
setBrightness(255);
}
/////////////////////////////////////////////////////
//This is Sequence 2
////////////////////////////////////////////////////
void sequence2(){
Serial.println("button 2");
for (int i = 0; i < 4; i++)
{
bitSet(leds, i);
updateShiftRegister2();
delay(50);
for (byte b = 0; b < 255; b++)
{
setBrightness(b);
delay(20);
}
Serial.println(count);
if(count>=3)
{
Serial.println("Count =");
Serial.print(count);
leds = 0;
//for (int i = 0; i < 4; i++)
{
bitSet(leds, -5);
updateShiftRegister2();
delay(50);
setBrightness(255);
}
count = 0;
break;
}
if (justpressed[1] && btnOn && (count >=3)) {
leds = 0;
break;
}
}
//bitSet(leds,0);
//delay(1000);
sr1Colour = 2; //Set SR1 to red
leds = 0;
//switch_value = 0;
setBrightness(255);
}
/////////////////////////////////////////////////////
//This is Sequence 3
////////////////////////////////////////////////////
void sequence3(){
Serial.println("button 1");
for (int i = 0; i < 4; i++)
{
bitSet(leds, i);
updateShiftRegister3();
delay(50);
for (byte b = 0; b < 255; b++)
{
setBrightness(b);
delay(20);
}
Serial.println(count);
if(count>=3){
Serial.println("Count =");
Serial.print(count);
leds = 0;
//for (int i = 0; i < 4; i++)
{
bitSet(leds, -5);
updateShiftRegister3();
delay(50);
setBrightness(255);
}
count = 0;
break;
}
if (justpressed[0] && btnOn && (count >=3)) {
Serial.println("Button 0 pressed again");
count;
leds = 0;
setBrightness(255);
break;
}
}
//bitSet(leds,0);
//delay(1000);
sr1Colour = 2; //Set SR1 to red
leds = 0;
//switch_value = 0;
setBrightness(255);
}
/////////////////////////////////////////////////////
//This is Sequence 4
////////////////////////////////////////////////////
void sequence4(){
Serial.println("button 1");
for (int i = 0; i < 4; i++)
{
bitSet(leds, i);
updateShiftRegister3();
delay(50);
for (byte b = 0; b < 255; b++)
{
setBrightness(b);
delay(20);
}
Serial.println(count);
if(count>=3){
Serial.println("Count =");
Serial.print(count);
leds = 0;
//for (int i = 0; i < 4; i++)
{
bitSet(leds, -5);
updateShiftRegister4();
delay(50);
setBrightness(255);
}
count = 0;
break;
}
if (justpressed[0] && btnOn && (count >=3)) {
Serial.println("Button 0 pressed again");
count;
leds = 0;
setBrightness(255);
break;
}
}
//bitSet(leds,0);
//delay(1000);
sr1Colour = 1; //Set SR1 to Green
leds = 0;
//switch_value = 0;
setBrightness(255);
}
/////////////////////////////////////////////////////
//This is Sequence 5
////////////////////////////////////////////////////
void sequence5(){
Serial.println("button 1");
for (int i = 0; i < 4; i++)
{
bitSet(leds, i);
updateShiftRegister5();
delay(50);
for (byte b = 0; b < 255; b++)
{
setBrightness(b);
delay(20);
}
Serial.println(count);
if(count>=3){
Serial.println("Count =");
Serial.print(count);
leds = 0;
//for (int i = 0; i < 4; i++)
{
bitSet(leds, -5);
updateShiftRegister5();
delay(50);
setBrightness(255);
}
count = 0;
break;
}
if (justpressed[0] && btnOn && (count >=3)) {
Serial.println("Button 0 pressed again");
count;
leds = 0;
setBrightness(255);
break;
}
}
//bitSet(leds,0);
//delay(1000);
leds = 0;
//switch_value = 0;
setBrightness(255);
}
/////////////////////////////////////////////////////
//This is Sequence 6
////////////////////////////////////////////////////
void sequence6(){
Serial.println("button 1");
for (int i = 0; i < 4; i++)
{
bitSet(leds, i);
updateShiftRegister6();
delay(50);
for (byte b = 0; b < 255; b++)
{
setBrightness(b);
delay(20);
}
Serial.println(count);
if(count>=3){
Serial.println("Count =");
Serial.print(count);
leds = 0;
//for (int i = 0; i < 4; i++)
{
bitSet(leds, -5);
updateShiftRegister6();
delay(50);
setBrightness(255);
}
count = 0;
break;
}
if (justpressed[0] && btnOn && (count >=3)) {
Serial.println("Button 0 pressed again");
count;
leds = 0;
setBrightness(255);
break;
}
}
//bitSet(leds,0);
//delay(1000);
leds = 0;
//switch_value = 0;
setBrightness(255);
}
/////////////////////////////////////////////////////
//This is Sequence 7
////////////////////////////////////////////////////
void sequence7(){
Serial.println("button 1");
for (int i = 0; i < 4; i++)
{
bitSet(leds, i);
updateShiftRegister7();
delay(50);
for (byte b = 0; b < 255; b++)
{
setBrightness(b);
delay(20);
}
Serial.println(count);
if(count>=3){
Serial.println("Count =");
Serial.print(count);
leds = 0;
//for (int i = 0; i < 4; i++)
{
bitSet(leds, -5);
updateShiftRegister7();
delay(50);
setBrightness(255);
}
count = 0;
break;
}
if (justpressed[0] && btnOn && (count >=3)) {
Serial.println("Button 0 pressed again");
count;
leds = 0;
setBrightness(255);
break;
}
}
//bitSet(leds,0);
//delay(1000);
leds = 0;
//switch_value = 0;
setBrightness(255);
}
/////////////////////////////////////////////////////
//This is Sequence 8
////////////////////////////////////////////////////
void sequence8(){
Serial.println("button 1");
for (int i = 0; i < 4; i++)
{
bitSet(leds, i);
updateShiftRegister8();
delay(50);
for (byte b = 0; b < 255; b++)
{
setBrightness(b);
delay(20);
}
Serial.println(count);
if(count>=3){
Serial.println("Count =");
Serial.print(count);
leds = 0;
//for (int i = 0; i < 4; i++)
{
bitSet(leds, -5);
updateShiftRegister8();
delay(50);
setBrightness(255);
}
count = 0;
break;
}
if (justpressed[0] && btnOn && (count >=3)) {
Serial.println("Button 0 pressed again");
count;
leds = 0;
setBrightness(255);
break;
}
}
//bitSet(leds,0);
//delay(1000);
leds = 0;
//switch_value = 0;
setBrightness(255);
}
/////////////////////////////////////////////////////
//This is Final Sequence 1
////////////////////////////////////////////////////
void sequenceFinal1(){
sequence5();
sequence7();
}
/////////////////////////////////////////////////////
//This is Final Sequence 2
////////////////////////////////////////////////////
void sequenceFinal2(){
sequence5();
sequence8();
}
/////////////////////////////////////////////////////
//This is Final Sequence 3
////////////////////////////////////////////////////
void sequenceFinal3(){
sequence6();
sequence7();
}
/////////////////////////////////////////////////////
//This is Final Sequence 4
////////////////////////////////////////////////////
void sequenceFinal4(){
sequence6();
sequence8();
}
cheers.