Go Down

Topic: I have some code that needs cleaned up. (fastLED based) (Read 264 times) previous topic - next topic


I am done hitting wall after wall on this project. I have all the code "working", but need it optimised so that the frame rate of the LEDs is not 1 fps. If you want to rework the code from scratch you can, but all I really need is the battery monitoring function to not interfere and slow down the fastLED display rate. I have the code attached, but it is not really well documented at this time.

Payment can be discussed if you are interested in this project.


Code: [Select]

void battMon() {
  if (millis() - period > time_now) {
    time_now = millis();

    // subtract the last reading:
    totalBatt = totalBatt - battSample[readBattIndex];
    // read from the sensor:
    battSample[readBattIndex] = analogRead(BATTERY_PIN);
    // add the reading to the totalBatt:
    totalBatt = totalBatt + battSample[readBattIndex];
    // advance to the next position in the array:
    readBattIndex = readBattIndex + 1;

    // if we're at the end of the array...
    if (readBattIndex >= numBattSample) {
      // ...wrap around to the beginning:
      readBattIndex = 0;

    // calculate the battAverage:
    battAverage = totalBatt / numBattSample;
    // send it to the computer as ASCII digits

You were really close. Don't sit there and wait - check whether it's time to do the check instead. So use an if statement instead of a while loop. Now you can just call it from loop(), and it will run every 1000 ms. Based on your description this was your problem.

Do note the different time calculation: this is now overflow-proof and continues to work when millis() overflows. Probably not an issue in this sketch, but it's a good idea to make it a habit of doing those things correctly.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up