// Define LED pins
#define ledPin1 11 // Pin for LED 1
#define ledPin2 10 // Pin for LED 2

// Define push button pins
int buttonPin1 = 13; // Pin for button 1
int buttonPin2 = 12; // Pin for button 2

// Variables to store the current brightness of LEDs
int brightness1 = 255;
int brightness2 = 255;

// Variables to store the step size for brightness change
int stepSize = 10;

void setup() {
  // Initialize LED pins as output
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  // Initialize push button pins as input
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);

  // Set initial brightness
  analogWrite(ledPin1, brightness1);
  analogWrite(ledPin2, brightness2);

  Serial.begin(9600);
}

void loop() {
  // Check button 1
  if (digitalRead(buttonPin1) == LOW) {
    // Increase brightness of LED 1
    brightness1 += stepSize;
    // Decrease brightness of LED 2
    brightness2 -= stepSize;
    // Check for limits
    if (brightness1 > 255) {
      brightness1 = 255;
    }
    if (brightness2 < 0) {
      brightness2 = 0;
    }

    Serial.print("Button 1: LED1 and LED2 values:");
    Serial.println(brightness1);
    Serial.println(brightness2);
  
    // Delay to avoid rapid button press
    delay(200);
  }

  // Check button 2
  if (digitalRead(buttonPin2) == LOW) {
    // Decrease brightness of LED 1
    brightness1 -= stepSize;
    // Increase brightness of LED 2
    brightness2 += stepSize;
    // Check for limits
    if (brightness1 < 0) {
      brightness1 = 0;
    }
    if (brightness2 > 255) {
      brightness2 = 255;
    }

    Serial.print("Button 2: LED1 and LED2 values:");
    Serial.println(brightness1);
    Serial.println(brightness2);
    // Delay to avoid rapid button press
    delay(200);
  }

   // Update LEDs with new brightness
  analogWrite(ledPin1, brightness1);
  analogWrite(ledPin2, brightness2); 
}
Loading
st-nucleo-c031c6