Skip to content

Instantly share code, notes, and snippets.

@rahulbot
Last active February 14, 2025 14:27
Show Gist options
  • Select an option

  • Save rahulbot/cef5bd061a77df102b65001162cfcc3c to your computer and use it in GitHub Desktop.

Select an option

Save rahulbot/cef5bd061a77df102b65001162cfcc3c to your computer and use it in GitHub Desktop.
Arduino example of using a data array to change the brightness of an LED
#define LED_PIN 6 // PWM pin connected to the LED
int numbers[20] = {10, 90, 30, 80, 50, 60, 10, 80, 0,
100, 20, 40, 70, 20 , 50, 40, 90, 20, 10, 0};
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// loop through every item in my data array
for (int i = 0; i < 20; i++) {
// Map the data (from 0 to 100) to PWM range (0 to 255)
int brightness = map(numbers[i], 0, 100, 0, 255);
// This uses PWM because Arduino knows I'm using a PWM-capable output pin
analogWrite(LED_PIN, brightness);
// hold brightness for 1 second
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment