strobe sequence

Hi,

i currently have this code running but will be adding more sequences to it… I have a button that is detected in the loop that will change the strobe sequence, the trouble is it is not reliable and does not always work, i think it needs some sort of debounce or button confirmation beep, to move to the next sequence can someone please help me out…

heres my current code…

  int pattern = 0;
  int patterna[18] = {100,500,100,500,100,500,300,500,300,500,300,500,100,500,100,500,100,1500};
  int patternb[8] =   {100,600,100,600,100,600,100,600};

/
  pinMode(0, OUTPUT); //LED on Model B
    }
void pattern1() {
  digitalWrite(0, HIGH);  
  delay(5);               
  digitalWrite(0, LOW);   
  delay(5000);             
}
void pattern2(){

for (int i = 0; i < 9; i++) {
    digitalWrite(0, HIGH);
    delay(patterna[i*2]);
    digitalWrite(0, LOW);
    delay(patterna[i*2+1]);
  }  
}

void pattern3() {
  for (int i = 0; i < 4; i++) {
    digitalWrite(0, HIGH);
    delay(patternb[i*2]);
    digitalWrite(0, LOW);
    delay(patternb[i*2+1]);
  }
}
void loop() {
  if (digitalRead(2) == LOW) {
    pattern = (pattern + 1)%3;
 delay(1000);
    digitalWrite(0, LOW);   
  }
  switch(pattern) {
    case 0: pattern1();
            break;
    case 1: pattern2();
            break;
    case 2: pattern3();
            break;
  }
}

Problem is that the key is only ‘tested’ now and then.
Better solved with interrupt. pin 2 is excellent for interrupt 0.

Rewrite. Use interrupt to switch sequece number.
(see reference… “attachinterrupt”)

you can avoid interrupts if you can also avoid using delay() within the different functions you call.

If you use interrupts, you still have to solve your bounce problem, or you will get unexpected results.

learn Blink Without Delay in the Arduino IDE and then learn State Change Detection for managing the pins.

…want to cheat ? take a look how ‘easy’ it is…
all your ‘delays’ will solve bouncing.

volatile int pattern = 0; // add "volatile" because of intr. change
volatile boolean keyPressed;
int patterna[18] = {100, 500, 100, 500, 100, 500, 300, 500, 300, 500, 300, 500, 100, 500, 100, 500, 100, 1500};
int patternb[8] =   {100, 600, 100, 600, 100, 600, 100, 600};
void setup()
{
  pinMode(0, OUTPUT); //LED on Model B
  attachInterrupt(0, changeSequence, FALLING); // this is pin 2 on UNO
}
void pattern1() {
  digitalWrite(0, HIGH);
  delay(5);
  digitalWrite(0, LOW);
  delay(5000);
}
void pattern2() {
  for (int i = 0; i < 9; i++) {
    digitalWrite(0, HIGH);
    delay(patterna[i * 2]);
    digitalWrite(0, LOW);
    delay(patterna[i * 2 + 1]);
  }
}
void pattern3() {
  for (int i = 0; i < 4; i++) {
    digitalWrite(0, HIGH);
    delay(patternb[i * 2]);
    digitalWrite(0, LOW);
    delay(patternb[i * 2 + 1]);
  }
}

void changeSequence()
{
    pattern = (pattern+1) % 3;
    keyPressed=true;
}

void loop() 
{
  if (keyPressed) // remove this if-statemant ??
  {
    digitalWrite(0, LOW);
    delay(100);
  }
  // remove above ??
  keyPressed=false;
  switch (pattern) {
    case 0: pattern1();
      break;
    case 1: pattern2();
      break;
    case 2: pattern3();
      break;
  }
}

knut_ny:
all your 'delays' will solve bouncing.

No, not really. During delay() interrupts work as they should. So, if you have a bouncy button, well you will get unexpected results, as you cannot guarantee that your interrupt fires only ONCE for your button press.

If you insist on using an interrupt, you can debounce the interrupt function, something like this:

void changeSequence() //interrupt with debounce
{
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 50)
  {
    pattern = (pattern+1) % 3;
    keyPressed=true;
  }
  last_interrupt_time = interrupt_time;
}

knut_ny:
…want to cheat ? take a look how ‘easy’ it is…
all your ‘delays’ will solve bouncing.

volatile int pattern = 0; // add "volatile" because of intr. change

volatile boolean keyPressed;
int patterna[18] = {100, 500, 100, 500, 100, 500, 300, 500, 300, 500, 300, 500, 100, 500, 100, 500, 100, 1500};
int patternb[8] =  {100, 600, 100, 600, 100, 600, 100, 600};
void setup()
{
  pinMode(0, OUTPUT); //LED on Model B
  attachInterrupt(0, changeSequence, FALLING); // this is pin 2 on UNO
}
void pattern1() {
  digitalWrite(0, HIGH);
  delay(5);
  digitalWrite(0, LOW);
  delay(5000);
}
void pattern2() {
  for (int i = 0; i < 9; i++) {
    digitalWrite(0, HIGH);
    delay(patterna[i * 2]);
    digitalWrite(0, LOW);
    delay(patterna[i * 2 + 1]);
  }
}
void pattern3() {
  for (int i = 0; i < 4; i++) {
    digitalWrite(0, HIGH);
    delay(patternb[i * 2]);
    digitalWrite(0, LOW);
    delay(patternb[i * 2 + 1]);
  }
}

void changeSequence()
{
    pattern = (pattern+1) % 3;
    keyPressed=true;
}

void loop()
{
  if (keyPressed) // remove this if-statemant ??
  {
    digitalWrite(0, LOW);
    delay(100);
  }
  // remove above ??
  keyPressed=false;
  switch (pattern) {
    case 0: pattern1();
      break;
    case 1: pattern2();
      break;
    case 2: pattern3();
      break;
  }
}

thanks this seems to work well…

thanks for the quick answers to this…