0% found this document useful (0 votes)
3 views

Tri-ColourLEDLabSampleCode

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)
3 views

Tri-ColourLEDLabSampleCode

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
You are on page 1/ 3

// Lab03 - PWM and ISRs

// [email protected]

#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:
Serial.begin(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: https://www.arduino.cc/reference/en/language/functions/external-
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
// Serial.print("Press (freeze).... counter is: ");
// Serial.println( counter );
// delay(1000); // freeze lights of LED
// }
//
// while ( counter > 0 && counter < 10 ) {
// if( digitalRead(pushButton) == LOW ) {
// counter++;
// Serial.print("Another Press (random)... counter is: ");
// Serial.println( 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
// Serial.print( "cycleRBG() ISR\tcounter: ");
// Serial.println( 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);
//}

You might also like