Hi! got some problems with my newbie code.

Hi there!

I've been playing around with Arduino for the first time, working on a project that makes three groups of LEDs do three different things.

Group 1 is 7 LEDs that chase left to right. (in practice it's actually a ring of LEDs that chase in a circle)

Group 2 is 1 LED that fades in and out.

Group 3 is 2 LEDs that flicker like candles.

I have the code.... let's say "working", but there is room for improvement.

  1. I'd like group 1's chase to speed up and slow down. I really have no idea how to do this, and it's my main problem. I'd also love it if I could make this group of LEDs quickly fade out, but again, I have no idea how to implement this.

  2. The chase itself seems glitchy, and not smooth. Like it stutters, almost.

Here's my current code, which has a lot of stuff I've cannibalised from various example code, and tried to edit:

// -----Definitions
#define GLOW 0
#define DIM 1
#define UP true
#define DOWN false


// ----CONSTANTS (won't change)

const int minPWM = 0;
const int maxPWM = 255;
const byte pwmLED = 9;

//------- VARIABLES (will change)

byte fadeDirection = GLOW;
int fadeValue = 0;
int flickerPin1 = 10;
int flickerPin3 = 11;
byte fadeIncrement = 5;
unsigned long previousFadeMillis;
unsigned long previouschaseLEDMillis=0;
unsigned long otherinterval = 30;
int fadeInterval = 20;
byte chaseLEDpins[] = {2,3,4,5,6,7,8};
int chaseLEDstate=0x01;
boolean direction=UP;

//========

void setup() {
  
  pinMode(12, INPUT_PULLUP);
 
 
  for (int x=0; x < 7; x++)
    pinMode(chaseLEDpins[x], OUTPUT);

   
  analogWrite(pwmLED, fadeValue); 

  pinMode(flickerPin1, OUTPUT);

pinMode(flickerPin3, OUTPUT);
}

void doTheFade(unsigned long thisMillis) {
  if (thisMillis - previousFadeMillis >= fadeInterval) {
    if (fadeDirection == GLOW) {
      fadeValue = fadeValue + fadeIncrement;  
      if (fadeValue >= maxPWM) {
        fadeValue = maxPWM;
        fadeDirection = DIM;
      }
    } else {
      fadeValue = fadeValue - fadeIncrement;
      if (fadeValue <= minPWM) {
        fadeValue = minPWM;
        fadeDirection = GLOW;
      }
    }
    analogWrite(pwmLED, fadeValue);  
    previousFadeMillis = thisMillis;
  }
}

//=======

void loop() {
 
  for (int x=0; x < 7; x++)
    digitalWrite(chaseLEDpins[x], bitRead(chaseLEDstate,x));
 
  
  if (digitalRead(12) == LOW)
    turnOnAll();
 
 
  unsigned long currentMillis = millis();
  if ((unsigned long)(currentMillis - previouschaseLEDMillis) >= otherinterval) {
    
 
    
    previouschaseLEDMillis = currentMillis;
 
    if (direction==UP) {
      
      chaseLEDstate = chaseLEDstate << 1;
    
      if (chaseLEDstate == 0x80) {
       
        chaseLEDstate = 0x00;
        direction = DOWN;
      }
    }
    else {
      
      chaseLEDstate = chaseLEDstate >> 1;
     
      if (chaseLEDstate == 0x00) {
        
        chaseLEDstate = 0x01;
        direction = UP;
      }
    }
 {
  
  unsigned long currentMillis = millis();
    
  doTheFade(currentMillis);
 
} {
analogWrite(flickerPin1, random(120)+135);

analogWrite(flickerPin3, random(120)+135);
delay(random(100));
}}
}
 
void turnOnAll() {
 
  while (digitalRead(12)==LOW) {
    for (int x=0; x< 6; x++)
      digitalWrite(chaseLEDpins[x], HIGH);
  }
} 
//=====END

Any help would be great!
Thanks!!!

Welcome to the forums. If you don't want things to jitter, you can't use delay() since it blocks your code from doing other things. Check out the Blink Without Delay example in the IDE (File->examples->02.Digital->BlinkWithoutDelay). You can also read the [url=http://[several things at the same time[/url] tutorial

It will give you some code like this

const int minPWM = 0;
const int maxPWM = 255;
const byte pwmLED = 9;

const byte flickerPin[] = {10, 11};
const byte flickerCount = sizeof(flickerPin) / sizeof(flickerPin[0]);
byte fadeIncrement = 5;
unsigned long otherinterval = 30;
unsigned long fadeInterval = 20;
const byte chaseLEDPin[] = {2, 3, 4, 5, 6, 7, 8};
const byte chaseCount = sizeof(chaseLEDPin) / sizeof(chaseLEDPin[0]);
unsigned long chaseInterval = 200;


const byte buttonPin = 12;


//========


void setup() {


  pinMode(buttonPin, INPUT_PULLUP);
  for (int i = 0; i < chaseCount; i++) {
    pinMode(chaseLEDPin[i], OUTPUT);
  }


  for (int i = 0; i < flickerCount; i++) {
    pinMode(flickerPin[i], OUTPUT);
  }
}


void fade() {
  static unsigned long prevTime = 0;
  static byte fadeValue = 0;
  static int fadeIncrement = 5;
  if (millis() - prevTime >= fadeInterval) {
    prevTime = millis();
    fadeValue += fadeIncrement;
    if (fadeValue >= maxPWM) {
      fadeValue = maxPWM;
      fadeIncrement = -fadeIncrement;
    }
    if (fadeValue <= minPWM) {
      fadeValue = minPWM;
      fadeIncrement = -fadeIncrement;
    }
  }
  analogWrite(pwmLED, fadeValue);
}


void chase() {
  static unsigned long prevTime = 0;
  static byte chaseIdx = 0;
  static unsigned long chaseInterval = 0;
  
  if ( millis() - prevTime >= chaseInterval ) {
    // time to increment the chase
    prevTime = millis();
    digitalWrite(chaseLEDPin[chaseIdx], LOW);
    chaseIdx++;
    if (chaseIdx == chaseCount ) chaseIdx = 0;
    digitalWrite(chaseLEDPin[chaseIdx], HIGH);
  }
  chaseInterval = random(100) * 100;
}


void flicker() {
  static unsigned long prevTime = 0;
  static unsigned long interval = 0;
  if ( millis() - prevTime >= interval ) {
    // time to increment the chase
    prevTime = millis();
    for(int i=0; i< flickerCount; i++ ) {
      analogWrite(flickerPin[i], random(120) + 135);
    }
    interval = random(100) + 20;
  }
}


void loop() {


  // check button and block until released
  if (digitalRead(buttonPin) == LOW) {
    turnOnAll();
  }


  chase();
  fade();
  flicker();
}


void turnOnAll() {


  while (digitalRead(12) == LOW) {
    for (int i = 0; i < chaseCount; i++) {
      digitalWrite(chaseLEDPin[i], HIGH);
    }
  }
}

](http://"Demonstration code for several things at the same time - Project Guidance - Arduino Forum")

Oh my goodness! That runs AMAZING.

Thank you so much for the help!!! I see where I made mistakes now.