#statemachine isn't working propperly

6 messages · Page 1 of 1 (latest)

lunar violet
#

so i use atmel studio to program an avr128db48 microchip. the idea is that a led switches modes each time the switch is clicked. clicked once = off, switched the second time = on, switched the third time = off-on-off-on.......
I need to do this with enums and switch case and i thought that it went pretty well but it didnt work. so maybe somebody here knows whats wrong with the code.

#include <atmel_start.h>
#include <util/delay.h>
#define F_CPU 4000000 UL

int main(void)
{

typedef enum SWITCHSTATE
{
SWITCHED1,
SWITCHED2,
SWITCHED3
};

int a = 0;
int b = SWITCHED1;

while (1) 
{
    if (SWITCH_get_level(false))
    {
        a++;
        if (a == 3)
        {
            a = 0;
        }
    }
    if (a == 0)
    {
        b = SWITCHED1;
    }
        else if (a == 1)
        {
            b = SWITCHED2;
        }
            else if (a == 2)
            {
                b = SWITCHED3;
            }
    
    switch (b)
    {
        case INGEDRUKT1:
            LED_set_level(true);
        break;
        case INGEDRUKT2:
            LED_set_level(false);
        break;
        case INGEDRUKT3:
            LED_set_level(false);
            _delay_ms(250);
            LED_set_level(true);
            _delay_ms(250);
        break;
    }
}

}

desert crowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

lunar violet
#

!format

desert crowBOT
desert crowBOT
#

so i use atmel studio to program an avr128db48 microchip. the idea is that a led switches modes each time the switch is clicked. clicked once = off, switched the second time = on, switched the third time = off-on-off-on.......
I need to do this with enums and switch case and i thought that it went pretty well but it didnt work. so maybe somebody here knows whats wrong with the code.

#include <atmel_start.h>
#include <util/delay.h>
#define F_CPU 4000000 UL

int main(void) {
  typedef enum SWITCHSTATE { SWITCHED1, SWITCHED2, SWITCHED3 };

  int a = 0;
  int b = SWITCHED1;

  while (1) {
    if (SWITCH_get_level(false)) {
      a++;
      if (a == 3) {
        a = 0;
      }
    }
    if (a == 0) {
      b = SWITCHED1;
    } else if (a == 1) {
      b = SWITCHED2;
    } else if (a == 2) {
      b = SWITCHED3;
    }

    switch (b) {
      case INGEDRUKT1:
        LED_set_level(true);
        break;
      case INGEDRUKT2:
        LED_set_level(false);
        break;
      case INGEDRUKT3:
        LED_set_level(false);
        _delay_ms(250);
        LED_set_level(true);
        _delay_ms(250);
        break;
    }
  }
}
Meeldrig
lunar violet
#

[INGEDRUKT1,2 & 3 are already changed to SWITCHED1,2 & 3]