int ledPin1 = 10; // LED connected to digital pin 10
int ledPin2 = 11; // LED connected to digital pin 11
void setup() {
// nothing happens in setup
}
void loop() {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
It currently fades in and out infinitely, but I want I want it to to turn on with the use of a push button (pin2) without needing to be held and will automatically turn of itself after a wait of 30 seconds.
The code (chatGPT?) does not have a button on pin 2.
Try this...
void setup() {
int buttonPin = 2;
pinMode(buttonPin, INPUT_PULLUP);
while (digitalRead(buttonPin));
}
You would need to instead of putting it into a loop() {};
you can do something like this: (NOTE: I can't debug this so I don't know if this will work but give it a shot)
void setup() {
}
void loop() {
if(digitalRead(buttonPin1) == HIGH) {
fade();
}
}
void fade() {
long startTime = millis();
while((startTime + 30000) != millis()) {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
}
I can't either, but I can read the code and I see
long startTime = millis();
while((startTime + 30000) != millis()) {
and flag it as not a great idea, at the very least because it depends on matching a value of millis() exactly.
Given that the while body takes some significant time per fade up/down cycle, the odds of hitting that mark is next to pretty unlikely.
I suggest at least writing
long startTime = millis();
while (millis() - startTime < 30000) {
and you'll have to live with it not being 'xactly 30 seconds, as when the fade up/down starts over, it may have started just at, say, 29.9 seconds, but it won't stop until whatever time each cycle takes has elapsed.
The complete cycle takes about 15 seconds as it is, so without breaking my brain it looks like you'd get two or three cycles, 30 seconds or so but if you run short, something close to 45.
There are ways to do it with more precision. I think it's worth getting that code to sorta work albeit not strictly meeting the specified behaviour.
a7
Hi @tiganoodles ,
Welcome to the forum..
need to lose all use of the blocking delay function..
maybe something like this..
#define SECS 1000
#define BTN_PIN 2
const byte ledPin1 = 10; // LED connected to digital pin 10
const byte ledPin2 = 11; // LED connected to digital pin 11
unsigned long lastPress;
unsigned long debounceInterval = 50;
byte lastButton = 1;
unsigned long lastStep;
unsigned long stepInterval = 30; //30 ms
byte fadeValue = 0;
byte direction = 0; //0=up 1=down
byte fadeStep = 5;
bool running = false;
unsigned long timeStart;
unsigned long runInterval = SECS * 30;
void setup() {
Serial.begin(115200);
Serial.println("Ready..");
pinMode(BTN_PIN, INPUT_PULLUP);
}
void loop() {
unsigned long now = millis();
if (now - lastPress >= debounceInterval) {
byte b = digitalRead(BTN_PIN);
if (b != lastButton) {
lastButton = b;
lastPress = now; //start debounce
if (b == HIGH) {
//pressed and released..
if (!running) {
timeStart = now;
running = true;
Serial.println("Starting..");
}
}
}
}
if (running) {
if (now - timeStart >= runInterval) {
Serial.println("Stopping..");
running = false;
analogWrite(ledPin1, 0);
analogWrite(ledPin2, 0);
}
if (now - lastStep >= stepInterval) {
lastStep = now;
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
if (direction == 0 ) {
if (fadeValue == 255) {
direction = 1;
fadeValue -= fadeStep;
} else
fadeValue += fadeStep;
} else {
if (fadeValue == 0) {
direction = 0;
fadeValue += fadeStep;
} else
fadeValue -= fadeStep;
}
}
}
}
have fun.. ~q
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.