Attiny85 Random Switch Programm

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); 
}

Your problem may be that the code updates the LED's every time loop() is executed. You should probably only update the LED's when a change in state is detected.

EDIT: Are the LED's supposed to change state continuously when in random mode?

Hi, no actually I wanted the Leds staying in the state for longer time. Thats why I tried to manage that by using the modulo operator.

The modulus operator will cause the LED's to re-randomize every ~3.675 seconds. In fact you could skip the modulus and use:

if (++x == 35)
{
  x = 0;
  //do the randomizing here
}

Oh, and.. The result of calling "random(2)" will be either "0" or "1" and not "1" or "2", so your case labels should be changed to that.

Thanks a lot for your help. I changed the code, but the behavior is still not as expected.

if (++x == 35){
     x=0;
    int led = random(2);
      switch (led) {
      case 0:
      StateLight1=LOW;
      StateLight2=HIGH;
      break;
      case 1: 
      StateLight1=HIGH;
      StateLight2=LOW;
      break;
      }
    }

The LED1 is switched on the whole time. When x=35 LED2 is switched on for around 100ms and then is LED1 switched on again till its reaching the 35 again.

That is because the LED's are updated for every iteration of loop() regardless of whether it is necessary or not. You can declare "StateLight1" and "StateLight2" as global or only call "digitalWrite()" on the LED's when their state actually is supposed to change. You should also set "x=0" whenever "mode" is changed.

@Danois90 : Thanks very much for your help. I could finally made it all work with your last hint to call the digitalWrite only in case of a matching condition. To be honest I am still not really sure, why it did set the digitalwrite, even when the StateLight1/2 didnt change because of not matching a different condition.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.