0% found this document useful (0 votes)
8 views3 pages

PWM Control with ISRs and LEDs

This document provides code for a lab exercise involving PWM and Interrupt Service Routines (ISRs) using a Teensy microcontroller. It includes setup for RGB LED control, a push button for interaction, and various demo functions for cycling colors and randomizing LED outputs. The code outlines the structure for handling button presses and interrupts to manage LED states effectively.

Uploaded by

ryan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

PWM Control with ISRs and LEDs

This document provides code for a lab exercise involving PWM and Interrupt Service Routines (ISRs) using a Teensy microcontroller. It includes setup for RGB LED control, a push button for interaction, and various demo functions for cycling colors and randomizing LED outputs. The code outlines the structure for handling button presses and interrupts to manage LED states effectively.

Uploaded by

ryan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// Lab03 - PWM and ISRs

// [Link]@[Link]

#define MY_DELAY 800

//These Teensy pins are PWM


int greenLED = 20;//3.0V, ~33ohm or bigger - LEDs turn ON with very little current
so the 220 ohm that came with the tri-color LED are fine
int blueLED = 21;//2.3V, ~111ohm
int redLED = 23;//1.6V, ~189ohm

// Demo #3 and Demo #4


int pushButton = 10;
// Demo #3
int counter = 0;
// Demo #4
volatile int counter = 0;

void setup() {
// put your setup code here, to run once:
[Link](9600);

pinMode(blueLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);

// Demo #3 and Demo #4


pinMode(pushButton, INPUT);

//use this open circuited pin for a seed to the random number generator
pinMode(A0, INPUT);
randomSeed(analogRead(A0));

// Demo #4
//README: [Link]
interrupts/attachinterrupt/
//attachInterrupt( pushButton, cycleRGB, FALLING );
}

void loop() {
// put your main code here, to run repeatedly:

// TODO: see below for each of the demos

} //end loop()

//DEMO #1 - CYCLE RGB (Digital)


// digitalWrite(redLED, HIGH);
// delay(MY_DELAY);
// digitalWrite(redLED, LOW);
//
// digitalWrite(greenLED, true);
// delay(MY_DELAY);
// digitalWrite(greenLED, false);
//
// digitalWrite(blueLED, 1);
// delay(MY_DELAY);
// digitalWrite(blueLED, 0);

//DEMO #2 - RANDOMLY SET EACH OF: R,G,B (PWM)


// analogWrite(redLED, random(256));
// analogWrite(greenLED, random(256));
// analogWrite(blueLED, random(256));
// delay(MY_DELAY);

//DEMO #3 - COLOUR CYCLING WITH A PUSH-BUTTON SWITCH (PBS)


//void loop() {
// randomLights();
//
// // first press of pushbutton
// if ( digitalRead(pushButton) == LOW ) {
// counter++;
// if ( counter > 10 ) counter = 1; // reset counter after 10 presses
// [Link]("Press (freeze).... counter is: ");
// [Link]( counter );
// delay(1000); // freeze lights of LED
// }
//
// while ( counter > 0 && counter < 10 ) {
// if( digitalRead(pushButton) == LOW ) {
// counter++;
// [Link]("Another Press (random)... counter is: ");
// [Link]( counter );
// randomLights();
// }
// }
//} //end loop() - Demo #3
//
//void randomLights() {
// analogWrite(redLED, random(256));
// analogWrite(greenLED, random(256));
// analogWrite(blueLED, random(256));
// delay(MY_DELAY);
//}

//DEMO #4 - COLOUR CYCLING WITH AN INTERRUPT SERVICE ROUTINE (ISR)


//void loop() {
// randomLights();
//} //end loop() - Demo #4
//
//
void cycleRGB() {
// int onPin, offPin_1, offPin_2;
//
// counter++;
// //FIXME: no I/O in ISR; only here for teaching purposes
// [Link]( "cycleRBG() ISR\tcounter: ");
// [Link]( counter );
//
// int beacon = counter % 3;
//
// if ( beacon == 0 ) {
// onPin = redLED;
// offPin_1 = greenLED;
// offPin_2 = blueLED;
// } else if ( beacon == 1 ) {
// onPin = greenLED;
// offPin_1 = redLED;
// offPin_2 = blueLED;
// } else { // must be Blue
// onPin = blueLED;
// offPin_1 = redLED;
// offPin_2 = greenLED;
// }
//
// analogWrite(onPin, HIGH);
// analogWrite(offPin_1, LOW);
// analogWrite(offPin_2, LOW);
// delay(200);
//}

Common questions

Powered by AI

Inline comments in the code provide immediate contextual information about what specific lines or blocks of code are intended to do. This can clarify complex or non-intuitive programming constructs, making the code more accessible and understandable to other developers or even the original author in future. In the provided code, comments describe the function of PWM, input pins, demos, and the role of ISRs, guiding the reader through the various processes and enhancing maintainability .

The loop structure in Demo #3 serves to continually check the state of the push-button and update the LEDs based on the 'counter' value. This continuous loop allows the program to monitor button presses in real-time, updating the counter and cycling LED colors accordingly. Without such a loop, the push-button state would not be regularly checked, leading to non-responsive or delayed interactions. Therefore, the loop ensures that the system dynamically responds to user inputs .

The use of 'delay' functions can block program execution, freezing other operations during the delay period. In real-time systems like LED control, this may lead to sluggish response to user inputs, as the program cannot process new inputs or interrupts during a delay. Additionally, excessive delays can make the light cycles appear static, reducing the dynamism of the lighting effects. To mitigate this, timing operations might be handled by non-blocking code practices like using 'millis()' instead .

The interrupt service routine (ISR) in Demo #4 allows the program to respond immediately to an external event—in this case, the press of a push-button. The ISR 'cycleRGB' determines which LED should be lit based on the counter's value (calculated as 'counter % 3') and switches LEDs accordingly. This provides a non-blocking and real-time method of handling events compared to the standard loop approach, which iterates sequentially and can be slower in responding to external inputs due to potential delays or ongoing loop processes .

The 'volatile' keyword is used to indicate that the value of the 'counter' variable can change unexpectedly, particularly since it may be modified by an ISR concurrently with other parts of the program. This prevents the compiler from optimizing the variable’s usage, ensuring that the program always reads the actual value of 'counter' from memory rather than relying on possibly outdated cached values. In this setup, it ensures consistent and correct behavior of the color cycling process across different parts of the program .

Pulse Width Modulation (PWM) is used to control the brightness of LEDs by varying the duty cycle of the digital signal. In the given code, PWM is implemented using the 'analogWrite' function, which allows setting the brightness level of each LED by passing a random value between 0 and 255 to initialize different brightness levels. This is demonstrated in Demo #2 and randomLights function, where 'analogWrite' is called on 'redLED', 'greenLED', and 'blueLED' with 'random(256)' values .

The random number generator is seeded using 'randomSeed(analogRead(A0))', where 'A0' is an analog pin. This seeding is crucial for ensuring that each run of the code results in a different sequence of 'random(256)' calls for LED brightness levels. By using an analog read on an open pin (A0), noise levels are captured, providing a more unpredictable starting point for pseudo-random number generation. This is significant in LEDs operation to achieve a non-repetitive random sequence, enhancing the randomness of color output .

Demo #1 employs 'digitalWrite' to switch LEDs on and off in a sequence, with each LED remaining fully on or off (binary state) for a fixed delay, thereby achieving basic cycling through colors. However, this method is limited to on/off states and cannot adjust brightness levels. Compared to PWM-based operations, which allow subtle changes in LED brightness (enabling smooth transitions and effects), 'digitalWrite' lacks the ability to modulate intensity and control the nuances of color blending .

The push-button switch is used to change the LED colors by incrementing the 'counter' variable whenever the button is pressed. In Demo #3, the program reads the state of the push-button (connected to pin 10) with 'digitalRead'. Upon each button press (detected when 'LOW'), the counter increases, and random colors are set by calling the 'randomLights' function, simulating a manual control over random LED sequences. If the counter exceeds 10, it resets to 1, providing a cyclic control mechanism .

In the Arduino setup, digital pins are configured as outputs to drive the LEDs. Pin 20 is assigned to the green LED, pin 21 to the blue LED, and pin 23 to the red LED. They are initialized as output pins using 'pinMode'. These pins control the respective LEDs by toggling HIGH or LOW signals—HIGH to turn on the LED and LOW to turn it off. This setup allows individual control over each LED's state, enabling various demos like RGB cycling and random light display .

You might also like