My aim is to get an LED to slowly brighten to full brightness (255) taking 4 seconds, and then decrease to 0 brightness taking 6 seconds (10 second cycle). I want this to go on for 8 minutes so this cycle would have to happen 48 times. I have the coding for the LED brightness changing which I will paste below: I was wondering if anyone could edit the code for me/add in code to get it to repeat the cycle I already have 48 times and then stop. I have tried googling but I keep messing up the code every time I change it.
#define LED_PIN 5
#define FINISH_PIN 6
#define UP_TIME 4 // In seconds
#define DOWN_TIME 6
uint8_t up_delay = ((UP_TIME * 1000) / 255);
uint8_t down_delay = ((DOWN_TIME * 1000) / 255);
#define ITERATIONS 6
void setup ()
{
pinMode(LED_PIN, OUTPUT);
pinMode(FINISH_PIN, OUTPUT);
}
void loop()
{
uint8_t i = 0;
while (i < ITERATIONS)
{
// Starting brightness
uint8_t brightness = 0;
while (brightness != 255)
{
brightness++;
analogWrite(LED_PIN, brightness);
delay(up_delay);
}
while (brightness != 0)
{
brightness--;
analogWrite(LED_PIN, brightness);
delay(down_delay);
}
i++;
}
digitalWrite(FINISH_PIN, HIGH);
while (1);
}
To repeat the whole cycle 48 times, im guessing you put loops inside another for loop that loops 48 times? I know there isn’t really a way to stop the program (Arduino programmes just keep running void loop forever). But I could add this at the end?:
analogWrite(led,0);
do{
}while(true);
I anyone could help me that would be AMAZING!! Thankyou
if you don't want this forever, then put the code in the setup and and a for loop around it to repeat it 48 times
for (int i = 0; i < 48; i++) {
for (int b = 0; b <= 255; b++) {
analogWrite(LED_PIN, b);
delay(up_delay);
}
for (int b = 254; b >= 0; b--) {
analogWrite(LED_PIN, b);
delay(down_delay);
}
}
the englobing for loop repeats 48 times
then you have 2 for loop inside, the first one counts up from 0 to 255 and the second one counts down from 254 (to not repeat 255 twice) to 0
#include <multiMap.h>
#include <timeObj.h>
#define LED_PIN 5
#define FINISH_PIN 6
#define UP_TIME 4 // In seconds
#define DOWN_TIME 6 // In seconds
#define FULL_TIME (UP_TIME+DOWN_TIME) // In seconds
#define FRAMERATE 20 // Frames/sec
#define FINISH_TIME 8 // Minutes for this to run.
multiMap brightnessMapper; // A non-linear mapper for brightness values (per ms)
timeObj changeTimer(1000.0/FRAMERATE); // A timer to tell us when to change the brightness.
timeObj finishTimer(FINISH_TIME*60.0*1000); // A timer to tell us when all this nonsens is to stop.
float msPerFrame; // How much time (ms) between brightness changes.
int iteration; // What interation of time are we on now?
void setup () {
pinMode(LED_PIN, OUTPUT); // Setup pulse pin.
pinMode(FINISH_PIN, OUTPUT); // Setup finish pin.
brightnessMapper.addPoint(0,0); // Start brightness at zero (LED off).
brightnessMapper.addPoint(UP_TIME*1000,255); // After up time, full on.
brightnessMapper.addPoint(FULL_TIME*1000,0); // At the end. (LED off again)
msPerFrame = 1000.0/FRAMERATE; // Calcualte our time, in ms, per bringtness change.
iteration = 0; // Start the interation count at 0.
}
void loop() {
int brightness;
float currentTime;
if (changeTimer.ding()) { // If the change timer has expired..
changeTimer.start(); // We restart it.
currentTime = iteration * msPerFrame; // Caluclate the current time into this pattern.
brightness = round(brightnessMapper.map(currentTime)); // Using current time, map to the brightness we want.
//Serial.println(iteration);
analogWrite(LED_PIN, brightness); // Set the LED to that brightness.
if (currentTime<FULL_TIME*1000.0) { // If we have time remaining..
iteration++; // Bump up the iteration.
} else { // else, we are done with the pattern.
iteration = 0; // Reset the interation back to zero.
}
}
if (finishTimer.ding()) { // If the finish timer has expired..
analogWrite(LED_PIN, 0); // Shut off the pulse LED.
digitalWrite(FINISH_PIN, HIGH); // Turn on the finished LED.
changeTimer.reset(); // Stop the change timer.
finishTimer.reset(); // Stop the finish timer.
}
}
You'll need to install LC_baseTools from the IDE library manger to compile it.