1

I'm working on a project with a starry sky, based on fiberoptics and 5 leds.

The code should work this way:

Start on average brightness, then go up or down according to a set of randomly chosen preset values. (min, max, wait time etc) and switch direction after the wait period on either its min/max is completed. When it reaches the average again, a new set of preset values should be assigned.

The updating of the leds should be done every set interval, so the change speed can be adjusted.

The leds go on, but nothing seems to change after that. Have I done something wrong? No errors given in the Arduino IDE.

(it's my first coding project, there will probably be some beginner mistakes or workarounds)

// Declare pins
int LED01           = 3;
int LED02           = 5;
int LED03           = 6;
int LED04           = 9;
int LED05           = 11;

// Time variables
unsigned long currentTime;
unsigned long waitingTime[5];
unsigned long lastRound;

// LED Variables
int brightMin [5]   =       { 0,    0,    0,    0,      0     };    // Minimum brightness
int brightAvg [5]   =       { 200,  230,  210,  210,    235   };    // Average brightness
int brightMax[5]    =       { 0,    0,    0,    0,      0     };    // Maximum brightness
int timeWaitLow[5]  =       { 0,    0,    0,    0,      0     };    // Waiting time
int timeWaitAvg[5]  =       { 0,    0,    0,    0,      0     };    // Waiting time
int timeWaitMax[5]  =       { 0,    0,    0,    0,      0     };    // Waiting time
int increment[5]    =       { 0,    0,    0,    0,      0     };    // Increment in brightness
int currentBright[5]=       { 200,  230,  210,  210,    235   };    // Current brightness

// LED Variables Possibilities
int brightMinPos[5] =       { 0,   20,    40,   0,      20    };    // Minimum brightness possibilities
int brightMaxPos[5] =       { 240,  250,  230,  225,    250   };    // Maximum brightness possibilities
int timeLowPos[5]   =       { 3000, 5000, 4000, 40000,  20000 };    // Waiting time on low brightness possibilities
int timeHighPos[5]  =       { 3000, 1000, 500,  2000,   4000  };    // Waiting time on high brightness possibilities
int timeAvgPos[5]   =       { 3000, 5000, 4000, 40000,  20000 };    // Waiting time on average brightness possibilities
int incrementPos[5] =       { 10,    20,    30,    40,      20     };    // increment in brightness possibilities

// Variables
bool needsUpdate[5]  =       { true, true, true, true,   true    };  // Does the led need a settings update?
bool reachedAvg[5]   =       { true, true, true, true,   true    };  // Has it reached the middle brightness?
bool reachedLow[5]   =       { false, false, false, false,  false};  // Has it reached the Lowest brightness?
bool reachedMax[5]   =       { false, false, false, false,  false};  // Has it reached the Highest brightness?
bool waitingAvg[5]   =       { false, false, false, false,  false};  // Is it waiting at the middle brightness?
bool waitingLow[5]   =       { false, false, false, false,  false};  // Is it waiting at the lowest brightness?
bool waitingMax[5]   =       { false, false, false, false,  false};  // Is it waiting at the Highest brightness?

// Directional variables
bool directionLED[5];

// Random variables
long randomNumber;                                                  //  Long to store random number

// counters
int i=0;


void setup() {
  Serial.begin (9600);                                              //  Start serial
  randomSeed(analogRead(A0));                                       //  Set randomseed 
  lastRound = millis();

// Declare outputs
  pinMode (LED01, OUTPUT);
  pinMode (LED02, OUTPUT);
  pinMode (LED03, OUTPUT);
  pinMode (LED04, OUTPUT);
  pinMode (LED05, OUTPUT);
}



// Check if it should stop at bottom, middle or top
void checkStopPoint(){
  for (i=0;i<5;i++){
    if (currentBright[i] <= brightMin[i] && reachedLow == false) {                         // is it at the lowest brightness?
      reachedLow[i] = true;
      waitingTime[i] = millis();
    }else if (currentBright[i] == brightAvg[i] && reachedAvg == false) {                   // is it at the average brightness?
      reachedAvg[i] = true;
      waitingTime[i] = millis();
    }else if (currentBright[i] >= brightMax[i] && reachedMax == false) {                   // is it at the highest brightness?
      reachedMax[i] = true;
      waitingTime[i] = millis(); 
    }
  }
  i=0;
}

void checkResetPoint(){
  for (i=0;i<5;i++){
    if (reachedLow[i] == true && currentTime-waitingTime[i] >= timeWaitLow){
      reachedLow[i] = false;
      directionLED[i] = true;
    }else if( reachedMax[i] == true && currentTime-waitingTime[i] >= timeWaitMax){
      reachedMax[i] = false;
      directionLED[i] = false;
    }else if ( reachedAvg[i] == true && currentTime-waitingTime[i] >= timeWaitAvg){
      reachedAvg[i] = false;
      needsUpdate[i] = true;
    }
  }
  i=0;
}


// update LED variables
void updateLEDVariables(){
  for (i=0; i>=5; i++){
    if (needsUpdate[i] == true){
      randomNumber      = random(5);
      brightMin[i]      = brightMinPos[randomNumber];                   // Set random value for minimum brightness
      randomNumber      = random(5);
      brightMax[i]      = brightMaxPos[randomNumber];                   // Set random value for maximum brightness
      randomNumber      = random(5);
      timeWaitLow[i]    = timeLowPos[randomNumber];                     // Set random value for low waiting time
      randomNumber      = random(5);
      timeWaitMax[i]    = timeHighPos[randomNumber];                    // Set random value for high waiting time
      randomNumber      = random(5);
      timeWaitAvg[i]    = timeAvgPos[randomNumber];                     // Set random value for average waiting time
      randomNumber      = random(5);
      increment[i]      = incrementPos[randomNumber];                   // Set random value for increment value
      randomNumber      = random(2);
      if (randomNumber == 1){
        directionLED[i] = true;
      }else{
        directionLED[i] = false;
      }
      needsUpdate[i]    = false;
      reachedAvg[i]     = false;
    }
  }
  i=0;
}

void writeToLED(){
  analogWrite(LED01, currentBright[0]);
  analogWrite(LED02, currentBright[1]);
  analogWrite(LED03, currentBright[2]);
  analogWrite(LED04, currentBright[3]);
  analogWrite(LED05, currentBright[4]);
}




void loop() {
  currentTime = millis();
  updateLEDVariables();
  checkStopPoint();
  checkResetPoint();

  if (currentTime - lastRound >= 1){

    for (i=0;i<5;i++){
      if (directionLED[i] == true && !reachedLow && !reachedAvg[i] && !reachedMax[i] && !needsUpdate[i]){
        currentBright[i] = currentBright[i]+increment[i];                         // Go 1 value up
      }else if (directionLED[i] == false && !reachedLow[i] && !reachedAvg[i] && !reachedMax[i] && !needsUpdate[i]){
        currentBright[i] = currentBright[i]-increment[i];                         // Go 1 value down
      }
    }
  i=0;
  writeToLED();
  lastRound = millis();
  }
}
  • Add print statements in your code, to know where the program is, and what values certain variables are. (you can increase times to easier see what is happening). – Michel Keijzers Mar 4 at 0:23
  • 1
    to make it easier, start with ONE LED - since you'll be outputting debug statements very quickly – Jaromanda X Mar 4 at 0:25
  • You have three of these: "currentTime-waitingTime[i] >= timeWaitLow", but "timeWaitLow" and the others are an array. You use "!reachedLow", but again, that is an array. After fixing those, the problem remains. In the settings, enable more compiler output and set the level of compiler warnings higher. Compile the sketch, and scroll up to see a few problems. Which arduino board do you use? A value of 40000 does not fit in a integer for an arduino uno. I would update the leds between 20 and 50Hz (a fixed value), and the rest is math. Can you show a schematic or tell how the leds are connected? – Jot Mar 4 at 1:24
  • upvote for properly formatted code – jsotola Mar 4 at 4:16
  • tip: if( reachedMax[i] == true) is the same as if( true == true) .................. so all you need is if( reachedMax[i]) ..... the converse is if( !reachedMax[i]) – jsotola Mar 4 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.