Hi,
I read a lot of really nice information here and a lot of stuff allready helped me a lot.
But now I have got a problem with a program on a ATTiny85 Digispark Board.
The Digispark is programmed with the Arduino Software and is running at 16,5Mhz.
I want to switch through three cases with a switch connected to Port 4 with activated internal pullup. This part actually works quite well. Within one of the switch cases I want to switch on two LEDs in random combination.
I always try to use the EVA principle in my codes. The problem is, that if I am in the mode of random combination of two LEDs, one LED just nearly stays on the whole time and the other LED ist switched on for around 100ms. I dont get the reason for that behavior. Maybe someone can help me out with this....I would really appreciate that.
enum {NORMALMODE, DARKMODE, TIMEMODE};
int mode = NORMALMODE;
int x;
int LightPin1 = 3;
int LightPin2 = 2;
int buttonPin = 4;
void setup() {
pinMode(LightPin1, OUTPUT);
pinMode(LightPin2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
boolean buttonPressed()
{
static boolean lastButtonState;
boolean buttonState=digitalRead(buttonPin);
if (buttonState!=lastButtonState)
{
lastButtonState=buttonState;
if (buttonState==LOW) return true;
}
return false;
}
void loop() {
if (buttonPressed())
{
mode++;
if (mode>TIMEMODE) mode=NORMALMODE;
}
int StateLight1;
int StateLight2;
int i=0;
if (mode==NORMALMODE){
StateLight1=1;
StateLight2=1;
delay(100);
}else if(mode==DARKMODE){
StateLight1=0;
StateLight2=0;
delay(100);
}else if(mode==TIMEMODE){
if (x%35==0){
int led = random(2);
switch (led) {
case 1:
StateLight1=0;
StateLight2=1;
break;
case 2:
StateLight1=1;
StateLight2=0;
break;
}
}
if (x==10000)
{
x=0;
}
delay(100);
}
x++;
//OUTPUT
digitalWrite(LightPin1,StateLight1);
digitalWrite(LightPin2,StateLight2);
delay(5);
}