I have a basic LED project I'm working on. The code is complete and it works perfectly on the Arduino Uno but it gives strange results on the Digispark. The project works very similar to a rear bicycle light. There are five LED's and a push button. Each time you click the button it cycles through 5 different light modes: All off, all on, blinking in unison, wave pattern, alternating. The code is below. The exact same code on my Uno works fine but when the Digispark is connected, it doesn't work right. For instance, instead of being all off, the first LED blinks rapidly. Then they all come on fine (but the first LED still flickers). Then they blink fine. For the alternating mode (opposites), all the LEDs flash very quickly and not in the correct pattern. And for the wave mode - this one is close but LED 1 and 2 have extra blinks out of order.
Again it all works perfect on the Uno but I can't figure out what to do to make it work with the Digispark. Has anyone else had issues like this? Or want to see how this code works for them? For the record, I've tried it on 2 different Digisparks so it's unlikely due to that.
/*
* Workbench Lights
* by:
* Andrew Zavala
*/
int led1 = 1;
int led2 = 2;
int led3 = 3;
int led4 = 4;
int led5 = 5;
int switchPin = 0;
int switchVal;
int switchVal2;
int buttonState;
int lightPosition = 0;
int lastLightPosition = 0;
void setup()
{
Serial.begin(9600);
pinMode(switchPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
buttonState = digitalRead(switchPin);
}
void loop()
{
CheckButton();
LightMode(lightPosition);
delay(10);
}
/******* FUNCTIONS *******/
// Checks status of the button and increments counter
void CheckButton()
{
switchVal = digitalRead(switchPin);
delay(10);
switchVal2 = digitalRead(switchPin);
if (switchVal == switchVal2)
{
Serial.println("matching values...");
if (switchVal != buttonState)
{
Serial.println("value doesnt match original button...");
if (switchVal == HIGH)
{
lightPosition++;
Serial.println(lightPosition);
if (lightPosition > 4)
{
lightPosition = 0;
}
}
}
}
lastLightPosition = lightPosition;
}
// Determines the Light Pattern
void LightMode(int lightPos)
{
switch(lightPos)
{
case 0:
Serial.println("case 0 is active!");
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(350);
break;
case 1:
Serial.println("case 1 is active!");
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
delay(350);
break;
case 2:
Serial.println("case 2 is active!");
Blink();
break;
case 3:
Serial.println("case 3 is active!");
Opposites();
break;
case 4:
Serial.println("case 4 is active!");
Wave();
break;
}
}
/****** LIGHT PATTERNS ******/
void Blink()
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
delay(500);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(500);
}
void Wave()
{
digitalWrite(led4, LOW);
digitalWrite(led2, LOW);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(250);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
delay(250);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
delay(250);
digitalWrite(led4, LOW);
digitalWrite(led5, HIGH);
delay(250);
digitalWrite(led5, LOW);
delay(250);
}
void Opposites()
{
digitalWrite(led3, HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led4, LOW);
delay(500);
digitalWrite(led3, LOW);
digitalWrite(led1, LOW);
digitalWrite(led5, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led2, HIGH);
delay(500);
}