// Salman Taş 20191701044
// Define the input and outputs
#define PB1 2
#define PB2 3
#define LED 6
// State change detection bool values
bool pb1 = 0;
bool pb2 = 0;
bool pb1_last = 0;
bool pb2_last = 0;
int brightness = 0; // global brightness variable
void setup() {
// Put your setup code here, to run once:
// Set the I/O
pinMode(PB1, INPUT);
pinMode(PB2, INPUT);
pinMode(LED, OUTPUT);
// Start serial communication
Serial.begin(9600);
}
int modifyBrightness(bool increment) {
if (increment && brightness < 255)
{
brightness = min(brightness + 10, 255); // Increase brightness up to 255
return 0;
}
else if (!increment && brightness > 0)
{
brightness = max(brightness - 10, 0); // Decrease brightness up to 0
return 0;
}
return brightness;
}
void loop() {
// Put your main code here, to run repeatedly:
// Nested if statements used to avoid unwanted button press
pb1 = digitalRead(PB1);
if (pb1 != pb1_last) {
if (pb1) {
// Increase the brightness only once per button press
modifyBrightness(true); // call the func with true
analogWrite(LED, brightness); // aply brigtness
//serial output
Serial.print("Brightness: ");
Serial.println(brightness);
}
pb1_last = pb1;
}
pb2 = digitalRead(PB2);
if (pb2 != pb2_last) {
if (pb2) {
// Decrease the brightness only once per button press
modifyBrightness(false); // call the func with false
analogWrite(LED, brightness); // apply brightness
//serial output
Serial.print("Brightness: ");
Serial.println(brightness);
}
pb2_last = pb2;
}
}
// when I open the bounce effect my code increments 9 times
// even I use state change detection
// currently, the bounce effect is closed
// I couldn't figure it out