New programmer needs help with led patterns a push button and a potetiometer

Hi All
My first post here. I'm completely new to programming and need a little help please.

A brief synopsis of what I'm trying to do:
I would like to flash a number of patterns on a range of LED's currently six.
I would like the patterns to change on the push of a single button.
Once selected I would like the pattern to loop until the next button push.
I would like a potentiometer to vary the speed that the pattern runs at as it is running.

basically a simple set of fairy lights!

I've got a few different codes from the web and that I have written.
They either work separately but I cant combine the features I want into one code or they partialy work with some of the functionality I want.

This was my first attempt on my own its very simple. the patterns change on the push of the button but only flash once!

int buttonPin = 13;       // set up pinn 13 as button pin
int timer = 75;           // The higher the number, the slower the timing.
int state = 0;

void setup() 
{
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 5; thisPin < 13; thisPin++)  
  {
    pinMode(thisPin, OUTPUT);      
    pinMode (buttonPin, INPUT);
  }
}

void loop() 
{
  if (digitalRead(buttonPin))
  {
    if (state == 0)
    {
      patOne();
      state = 1;
    }
    else if (state == 1)
    {
      patTwo();
      state = 0;
    }
  }
}
      
      
      
void patOne()
 {
  for (int thisPin = 5; thisPin < 13; thisPin++)      // loop from the lowest pin to the highest:
    {
    digitalWrite(thisPin, HIGH);                     // turn the pin on:   
    delay(timer);                                      
    digitalWrite(thisPin, LOW);                      // turn the pin off:
    }
 }
 
 
void patTwo()
 {
  for (int thisPin = 12; thisPin >= 5; thisPin--)  // loop from the highest pin to the lowest:
    { 
    digitalWrite(thisPin, HIGH);                   // turn the pin on:
    delay(timer);
    digitalWrite(thisPin, LOW);                    // turn the pin off:
    }
 }

This one uses a counter but again the led sequences only flash once.

int buttonPin = 13;       // set up pinn 13 as button pin
int timer = 75;           // The higher the number, the slower the timing.
int counter = 0;
int switchVal = digitalRead(buttonPin);

void setup() 
{
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 5; thisPin < 13; thisPin++)  
  {
    pinMode(thisPin, OUTPUT);      
    pinMode (buttonPin, INPUT);
  }
}

void loop() 
{
  int switchVal = digitalRead(buttonPin);
    if(switchVal == HIGH)
    {
      counter ++;
    }
  if (digitalRead(buttonPin))
  {
    if (counter == 1)
    {
      patOne();
    }
    else if (counter == 2)
    {
      patTwo();
    }
    else if (counter ==3)
    {
      counter = 0;
    }
  }
}
      
      
      
void patOne()
{
  for (int thisPin = 5; thisPin < 13; thisPin++)      // loop from the lowest pin to the highest:
    {
    digitalWrite(thisPin, HIGH);                     // turn the pin on:   
    delay(timer);                                      
    digitalWrite(thisPin, LOW);                      // turn the pin off:
    }
 }
 
 
void patTwo()
 {
  for (int thisPin = 12; thisPin >= 5; thisPin--)  // loop from the highest pin to the lowest:
    { 
    digitalWrite(thisPin, HIGH);                   // turn the pin on:
    delay(timer);
    digitalWrite(thisPin, LOW);                    // turn the pin off:
    }
 }

I found the two following codes on the net and have them both running well

This code has the flash sequences I like. It controls the sequences through the use of a six way dip switch not the single button I want I would also like to change the use of delay to the use of millis but being the noob coder that I am I cant get my head round how to implement it !

// www.TheElectronicsHobbyist.com/blog
  // Natalia Fargasch Norman
  // LED control via DIP switches

  // Arduino pins used for the LEDs
  // LED1 13
  // LED2 12
  // LED3 11
  // LED4 10
  // LED5 9
  // LED6 8

  // Arduino pins used for the switches
  // S1 7
  // S2 6
  // S3 5
  // S4 4
  // S5 3
  // S6 2

  // State of each switch (0 or 1)
  int state[6];

  // Random values for LED state and delay
  long onoroff;
  long millisecs;

  // loop counters
  int i, j;

  // delay
  int d = 250;

  void setup() {
    // pins for LEDs are outputs
    // LEDs 1-6 on pins 13-8
    for (i = 13; i >= 8; i--) {
      pinMode(i, OUTPUT);
    }
    // pins for switches are inputs
    // switches 1-6 on pins 7-2
    for (i = 7; i >= 2; i--) {
      pinMode(i, INPUT);
    }
  }

  void loop() {
    for (i = 0, j = 7; i < 6, j >= 2; i++, j--) {
      state[i] = digitalRead(j);
    }

    if (state[0]) {
      // scroll right
      for (i = 13; i >= 8; i--) {
        digitalWrite(i, HIGH);
        delay(d);
        digitalWrite(i, LOW);
      }

    } else if (state[1]) {
      // scroll left
      for (i = 8; i <= 13; i++) {
         digitalWrite(i, HIGH);
         delay(d);
         digitalWrite(i, LOW);
       }

   } else if (state[2]) {
       // scroll in
       // light up LEDs on pins i and 8+(13-i)
       for (i = 13; i >= 11; i--) {
        digitalWrite(i, HIGH);
        digitalWrite(21 - i, HIGH);
        delay(d);
        digitalWrite(i, LOW);
        digitalWrite(21 - i, LOW);
      }

    } else if (state[3]) {
      // scroll out
      // light up LEDs on pins i and 8+(13-i)
      for (i = 11; i <= 13; i++) {
         digitalWrite(i, HIGH);
         digitalWrite(21 - i, HIGH);
         delay(d);
         digitalWrite(i, LOW);
         digitalWrite(21 - i, LOW);
       }

    } else if (state[4]) {
       // scroll back and forth
       for (i = 13; i >= 8; i--) {
        digitalWrite(i, HIGH);
        delay(d);
        digitalWrite(i, LOW);
      }
      for (i = 9; i <= 12; i++) {
        digitalWrite(i, HIGH);
        delay(d);
        digitalWrite(i, LOW);
      }

    } else if (state[5]) {
       // random
       for (i = 13; i >= 8; i--) {
        randomSeed(analogRead(i - 8));
        onoroff = random(0, 2);
        millisecs = random(0, 301);
        digitalWrite(i, onoroff);
        delay(millisecs);
      }      

    } else {
      // default: off
      for (i = 13; i >= 8; i--) {
        digitalWrite(i, LOW);
      }
    }
  }

This code I have also runs and it works well. I found it in a thread on here. I would like to use the sections of code that use the potetiometer to regulate the speed of the led pattern. Trouble is I dont understand how they work and how they change the speed as the pattern is flashing.

const int buttonPin = 2;     
const int ledPin1 =  13;      
int buttonState = 0;        

int leds[] = {3, 4, 5, 6, 7, 8, 9, 10};
#define NUMBER_OF_LEDS (sizeof(leds)/sizeof(int))

boolean flickbook[][NUMBER_OF_LEDS] = {
{  HIGH,  LOW,  LOW,  LOW,  LOW,  LOW, LOW,  LOW},       
{  LOW, HIGH,  LOW,  LOW,  LOW,  LOW, LOW,  LOW},
{  LOW,  LOW, HIGH,  LOW,  LOW,  LOW, LOW,  LOW},
{  LOW,  LOW, LOW,  HIGH,  LOW,  LOW, LOW,  LOW},
{  LOW,  LOW,  LOW,  LOW,  HIGH,  LOW, LOW,  LOW},      
{  LOW,  LOW, LOW,  LOW,  LOW,  HIGH, LOW,  LOW},
{  LOW,  LOW, LOW, LOW,  LOW,  LOW, HIGH,  LOW},
{  LOW,  LOW,  LOW, LOW,  LOW,  LOW, LOW,  HIGH}
};

#define FRAMES (sizeof(flickbook)/(sizeof(flickbook[0])))


int sensorPin = 0;

void setup()   {   
 
 pinMode(ledPin1, OUTPUT);      
 pinMode(buttonPin, INPUT);     
  
 for (int led=0; led<NUMBER_OF_LEDS; led++) {  
   pinMode(leds[led], OUTPUT);
 }    
}

void loop(){
 
   buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH) {       
   digitalWrite(ledPin1, HIGH);  
 } 
 else {
   
 long time = millis();
 
 for (int frame=0; frame<FRAMES; frame++) {
   for (int led=0; led<NUMBER_OF_LEDS; led++) {  
     digitalWrite(leds[led], flickbook[frame][led]);
   }
   int sensorValue = map(analogRead(sensorPin), 0, 1023, 0, 1000);
   while (sensorValue >= (millis() - time)) { 
       sensorValue = analogRead(sensorPin);   
   }
   time = millis();
 }
}
}

Wow that's a much longer post than I intended I hope my first post isn't either too green or too troublesome thanks for looking.

J*

If you want buttons to be responsive you can't use the delay() function, particularly in a loop. See the Blink Without Delay example for a method of detecting when a time period has expired. You'll need variables to keep track of which pattern is in effect, what step in the pattern is the current step, when that step started, and how long the step should last.

thanks for the reply
I've looked at the blink without delay example but dont understand how to implement it or put in the variables you suggest

this code lights half the leds permantly

int timer = 75;           // The higher the number, the slower the timing.
int state = 0;
int ledState = LOW;
long previousMillis = 0;
long interval = 75;  

void setup() 
{
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 5; thisPin < 13; thisPin++)  
  {
    pinMode(thisPin, OUTPUT);      
      }
}

void loop() 
{
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;
  
  for (int thisPin = 5; thisPin < 13; thisPin++)      // loop from the lowest pin to the highest:
    {
    if (ledState == LOW) {
      ledState = HIGH;
    }
    else {
      ledState = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(thisPin, ledState);
    }
}
}

and this code lights all the leds all the time

int timer = 75;           // The higher the number, the slower the timing.
int state = 0;
int ledState = LOW;
long previousMillis = 0;
long interval = 75;  

void setup() 
{
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 5; thisPin < 13; thisPin++)  
  {
    pinMode(thisPin, OUTPUT);      
      }
}

void loop() 
{
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;
  
  for (int thisPin = 5; thisPin < 13; thisPin++)      // loop from the lowest pin to the highest:
    {
    if (ledState == LOW) {
      digitalWrite(thisPin, HIGH);
    }
    else {
      if (ledState == HIGH)
      digitalWrite(thisPin, LOW);
    }
    
}
  }
}

there is no button I was just trying to get the pattern to work what have I done wrong? I cant figure it out or find a good example of a pattern code to pull apart and learn from.

jdlenton:
this code lights all the leds all the time

  for (int thisPin = 5; thisPin < 13; thisPin++)      // loop from the lowest pin to the highest:

{
   if (ledState == LOW) {
     digitalWrite(thisPin, HIGH);
   }
   else {
     if (ledState == HIGH)
     digitalWrite(thisPin, LOW);
   }

The code looks pretty close, but you aren't updating the value of ledState so it is doing the 'low to high' transition every time.

It took me a while to convince myself that the basic logic was right, because of the wonky indentation. Do yourself a favour and put every { and } on a line on their own, with matching pairs indented by the same amount.

Also, the logic 'if (state == low) ... else if (state == high)' is slightly flawed; the second 'if' is redundant since you don't intend the state to be anything other than high or low. In fact since your code actually ignores any other state, if a bug elsewhere caused the state to be any other value then this bit of code would silently fail. It's better to put it like this:

if(state == HIGH)
{
    state = LOW;
    // do stuff
}
else
{
    state = HIGH; 
    // do stuff
}

There are ways to express that logic more concisely but I think this is the clearest way to implement it.

Also, any variables holding time values should be of type unsigned long. You have a couple that are long i.e. signed.

thank you I'll give that a try when I get a chance.
School starts again tomorrow so its all planing to teach and getting back into the swing :slight_smile: