Hi newbie @codynutbrain
Kindly go through this
Keyword or
is a synonym for ||
for long time.
As and
for &&
In case you want something more "Beginner friendly", here is what I came up with :
int8_t led = 9;
int8_t button = 8;
int16_t brightness = 0;
int8_t fadeAmount = 1;
int8_t InvDir;
void setup() {
Serial.begin(115200);
Serial.println("LED Dimmer V2");
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
}
void loop() {
while (digitalRead(button) == LOW) {
InvDir = 1;
brightness += fadeAmount; // identical to brightness = brightness + fadeAmount
if (brightness < 0) {
brightness = 0;
}
else if (brightness > 255) {
brightness = 255;
}
analogWrite(led, brightness);
Serial.println(brightness);
delay(30);
}
if (InvDir) {
InvDir = 0;
fadeAmount = -fadeAmount;
Serial.println("Button Released");
}
}
This code however does block other activities...
Here is a simple non blocking version (no software debouncing though => hardware debouncing if required)
int8_t led = 9;
int8_t buttonPin = 8;
int16_t brightness = 0;
int8_t fadeAmount = 1;
uint8_t buttonState;
uint8_t prevBtnState = HIGH;
uint32_t currentMillis;
uint32_t previousMillis;
uint8_t intervalUp = 30;
void updateBrightness() {
brightness += fadeAmount;
if (brightness < 0) {
brightness = 0;
} else if (brightness > 255) {
brightness = 255;
}
analogWrite(led, brightness);
Serial.println(brightness);
}
void setup() {
Serial.begin(115200);
Serial.println("LED Dimmer V2");
pinMode(buttonPin, INPUT_PULLUP);
pinMode(led, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
if (prevBtnState == HIGH) {
updateBrightness();
Serial.println("Button Pressed");
}
else {
currentMillis = millis();
if (currentMillis - previousMillis > intervalUp) {
previousMillis = currentMillis;
updateBrightness();
Serial.println("New Brightness");
}
}
}
else {
if (prevBtnState == LOW) {
fadeAmount = -fadeAmount;
Serial.println("Button Released");
}
}
prevBtnState = buttonState;
}
And a cheap and easy debouncing solution :
int8_t led = 9;
int8_t buttonPin = 8;
int16_t brightness = 0;
int8_t fadeAmount = 1;
uint8_t buttonState;
uint8_t prevBtnState = HIGH;
uint32_t currentMillis;
uint32_t previousMillis;
uint8_t intervalUp = 30;
uint8_t debouncingDelay = 25;
void updateBrightness() {
brightness += fadeAmount;
if (brightness < 0) {
brightness = 0;
} else if (brightness > 255) {
brightness = 255;
}
analogWrite(led, brightness);
Serial.println(brightness);
}
void setup() {
Serial.begin(115200);
Serial.println("LED Dimmer V2");
pinMode(buttonPin, INPUT_PULLUP);
pinMode(led, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
if (prevBtnState == HIGH) {
updateBrightness();
previousMillis = millis();
Serial.println("Button Pressed");
delay(debouncingDelay);
}
else {
currentMillis = millis();
if (currentMillis - previousMillis > intervalUp) {
previousMillis = currentMillis;
updateBrightness();
Serial.println("New Brightness");
}
}
}
else {
if (prevBtnState == LOW) {
fadeAmount = -fadeAmount;
Serial.println("Button Released");
delay(debouncingDelay);
}
}
prevBtnState = buttonState;
}
Updates welcome...
For your reference,
Link: Wokwi Simulation
#include "Debounce.h"
#define pbPin 5
#define ledPin 3
#define millisec(t) (t * 1000UL)
bool input;
bool prevInput;
bool fade;
uint8_t brightness;
Debounce inputDebounce;
unsigned long startTime;
unsigned long repeatRate;
unsigned long debounceTime;
void InputCondition();
void Fade();
void PrintBrightness(uint8_t brightness);
void setup() {
Serial.begin(115200);
pinMode(pbPin, INPUT_PULLUP); // Pin Mode
pinMode(LED_BUILTIN, OUTPUT); // Pin Mode
repeatRate = millisec(10); // Button Repeat Rate 10 milliseconds
inputDebounce.setDebounceTime(20); // Debounce time 20 milliseconds
brightness = 0; // Initial brightness
fade = false; // Initial fade (TRUE = Fade-In, FALSE = Fade-Out)
digitalWrite(LED_BUILTIN, !fade); // Next Fade Status (ON = Fade-In, OFF = Fade-Out)
PrintBrightness(brightness); // Print brightness
}
void loop() {
InputCondition(); // Button Input Condition
Fade(); // Fade-In/Out
}
void InputCondition() {
bool pbValue = !digitalRead(pbPin); // Read Input
input = inputDebounce.debounce(pbValue); // Debounce Input
if (input & !prevInput) { // Risign Edge Detector
startTime = micros(); // Reload startTime
fade = !fade; // Toggle Fade (TRUE = Fade-In, FALSE = Fade-Out)
}
if (!input & prevInput) { // Falling Edge Detector
digitalWrite(LED_BUILTIN, !fade); // Next Fade Status (ON = Fade-In, OFF = Fade-Out)
}
prevInput = input;
}
void Fade() {
if (!input) return;
if (micros() - startTime < repeatRate) return; // Repeat Rate Detector
startTime = micros(); // Reload startTime
if (fade) { // Fade-In
if (brightness > 254) return; // brightness equal to 255, return
brightness++; // Fade-In
PrintBrightness(brightness); // Print brightness
} else { // Fade-Out
if (brightness < 1) return; // brightness equal to 0, return
brightness--; // Fade-Out
PrintBrightness(brightness); // Print brightness
}
analogWrite(ledPin, brightness); // Update brightness
}
void PrintBrightness(uint8_t brightness) {
Serial.print("Brightness:= ");
Serial.println(brightness);
}
MicroBeaut (μB)
this is exactly what i wanted thank you
Is that by any chance someone to do the homework for you ?
no this was for my grandpas train set and when he cant figure something out he sees if i can
I stand with you : many helpers (of good will, no argument there) design Heavy Duty Trucks when OP just requires a bicycle.
Below link may help with code by " gcjr"
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.