Skip to content

Instantly share code, notes, and snippets.

@seekshreyas
Created September 25, 2013 04:34
Show Gist options
  • Select an option

  • Save seekshreyas/6695191 to your computer and use it in GitHub Desktop.

Select an option

Save seekshreyas/6695191 to your computer and use it in GitHub Desktop.
# TUI Lab 2: Pulse Wave Modulation and Serial Input
/*
* Code for cross fading 3 LEDs and then taking user input
* to increase the stepwise intensity of the corresponding LED
* r for red, b for blue, g for gree
* by: [email protected]
* License: MIT
*/
// Output
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
// Program variables
int redVal = 255; // Variables to store the values to send to the pins
int greenVal = 0; // Initial values are Red full, Green and Blue off
int blueVal = 0;
int i = 0; // Loop counter
int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 0; // DEBUG counter; if set to 1, will write values back via serial
char userInput[100]; //get user input keys
char color;
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600); // ...set up the serial ouput on 0004 style
Serial.print("Enter the command as a combination of r/g/b keys");
}
// Main program
void loop()
{
i += 1; // Increment counter
memset(userInput, 0, 100);
readUserInput(userInput);
Serial.print(userInput);
for (int i=0; i<= sizeof(userInput); i++){
color = userInput[i];
// Serial.print("color");
Serial.print(color);
if (color == 'r'){
//red key
redVal = redVal+ 25.5;
if (redVal >= 255){
redVal = 0;
}
analogWrite(redPin, redVal); // Write current values to LED pins
Serial.print(redVal);
}
if (color == 'g'){
//red key
greenVal = greenVal + 25.5;
if (greenVal >= 255){
greenVal = 0;
}
analogWrite(greenPin, greenVal);
Serial.print(redVal);
}
if (color == 'b'){
//red key
blueVal = blueVal+ 25.5;
if (blueVal >= 255){
blueVal = 0;
}
analogWrite(bluePin, blueVal);
Serial.print(redVal);
}
if (color == 'f'){
}
if (DEBUG) { // If we want to read the output
Serial.print(i); // Serial commands in 0004 style
Serial.print("\t"); // Print a tab
Serial.print("R:"); // Indicate that output is red value
Serial.print(redVal); // Print red value
Serial.print("\t"); // Print a tab
Serial.print("G:"); // Repeat for green and blue...
Serial.print(greenVal);
Serial.print("\t");
Serial.print("B:");
Serial.println(blueVal); // println, to end with a carriage return
}
}
analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
void readUserInput(char *strArray){
int i = 0;
if (Serial.available()){
while(Serial.available()){
strArray[i] = Serial.read();
i++;
}
}else{
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment