Tony YuAnita ZhouJing WangLily LiuRose ZhangSiyang SuDonglin Wanglorraine lyu
Published

Running with RGB

Enjoy a marvelous running experience with our RGB LED light that reflects your running rhythm. May the rhythm be with you!

BeginnerFull instructions provided1 hour482
Running with RGB

Things used in this project

Hardware components

EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
Breadboard (generic)
Breadboard (generic)
×1
Tilt Switch, SPST
Tilt Switch, SPST
×1
Resistor 330 ohm
Resistor 330 ohm
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Jumper wires (generic)
Jumper wires (generic)
×6

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Custom parts and enclosures

img_0882_2_XwcUHKcYKD.JPG

Schematics

Schematics

Code

sketch_speedTest2 1.ino

C/C++
/*

 Hardware Required:
 * TI LaunchPad
 * Breadboard BoosterPack
 * Breadboard
 * RGB LED
 * 6x Jumper Wires
 * tilted switch
 * 1k resistor
 This example code is in the public domain.
*/
// You can put a resistor in between to protect your circuit
// but for low power LEDs it's usually okay to drive directly
// from the LaunchPad.

// use #define to rename our pins from numbers to readable variables
#define RED 19 // pin 19 is always PWM capable according to LaunchPad standard
#define GREEN 18 // may need to change this for your LaunchPad
#define BLUE 11 // may need to change this for your LaunchPad
#define delayTime 1 // delay between color changes, 10ms by default
#define tiltedSwitch 15 //the pin for the tilted switch

// introduce some global variables. These variables have a type
// (int) and a name. We can use the variables in any function that comes after
// Global variables are useful because we can save the state of a variable to
// use in later operations and functions.
int redVal;
int greenVal;
int blueVal;
int dir = 1; // the indicator for tilt state
int up = 0; //the total recent consequtive time when hand is up
int down = 0; //the total recent consequtive time when hand is down
int lastIndex = 0; //the flag to remember turns
int a[] = {75, 75, 75, 75, 75}; //The initial state of the 5 previous timespan of hand shake
int arrIndex = 0; //the index for update sum values in the array
int sum = 0; // the sum of one round(hand up and down one time)
int m = 0; //the mean time of recent 5 rounds

/* This is our setup function. We want to set our LED pins as OUTPUT.
 * We can also set them to HIGH at the beginning.
 */
void setup() {
 // We don't have to use pinMode() when using analogWrite() but it doesn't
 // hurt to use it, especially if we want to call digitalWrite() for the
 // same pin in the same sketch.
 pinMode(RED, OUTPUT);
 pinMode(GREEN, OUTPUT);
 pinMode(BLUE, OUTPUT);
 pinMode(tiltedSwitch, INPUT);
Serial.begin(9600); 
 // Test Red
 digitalWrite(RED, LOW);
 digitalWrite(GREEN, HIGH);
 digitalWrite(BLUE, HIGH);
 delay(500);
 // Test Green
 digitalWrite(RED, HIGH);
 digitalWrite(GREEN, LOW);
 digitalWrite(BLUE, HIGH);
 delay(500);
 // Test Blue
 digitalWrite(RED, HIGH);
 digitalWrite(GREEN, HIGH);
 digitalWrite(BLUE, LOW);
 delay(500);
}

/* In the loop function we will fade the brightness of the RGB LED
 * and simultaneously change the colors.
 */
void loop() {
int tiltState = digitalRead(tiltedSwitch); //read the tilt state of tilted switch
//the the hand's mothion has changed
if (tiltState != lastIndex) { 
  lastIndex = tiltState;
  //If hand is down, meaning one round ends, we need to reset the counters
  if (tiltState == 1) {
    sum = up+down;
    up = 0;
    down = 0;
    a[arrIndex] = sum;
    m = mean();
    Serial.print("\n    mean is ");
    Serial.print(m);
  }
}
//count the time for one motion (up or down)
if(tiltState == LOW) {
      /*dir == 0 hand up*/
      dir = 0; 
//      Serial.print("\n0");  
      up++; 
   } else {
     /*dir == 1 hand down*/
      dir = 1;
      down++;
//      Serial.print("\n1");
}

 int test = max(0, min((m - 15) * 7, 250));
 int tail = 0;
 if (m > 50) {
  tail = m - 50;
 }
 
 //if the frequency is more than 3 per sec
 //set the light to red
  if (m <= 18) {
    test = 0;
  }
  
 int redVal = test; // right light increases as frequency increases
 int greenVal = max (0, 255 - tail*10); 
 int blueVal = max(0, 255 - test); // blue light increase when frequency is smaller
 
 //when the frequency is smaller than 1 per second, set light to green.
  if (m > 60) {
  blueVal = 255;
  greenVal = 0;
 }
 
 //set light color
 analogWrite( RED, redVal );
 analogWrite( GREEN, greenVal );
 analogWrite( BLUE, blueVal );
 delay(15);
 arrIndex++;
 if (arrIndex == 5) {
  arrIndex = 0;
 }
}

//helper function for calculating mean 
int mean(){
  int sum = 0;
  for (int i = 0; i<5; i++){
    sum += a[i];
  }
  int mean = sum/5;
  return mean;
}

Credits

Tony Yu
1 project • 0 followers
Contact
Anita Zhou
1 project • 0 followers
Contact
Jing Wang
1 project • 0 followers
Contact
Lily Liu
1 project • 0 followers
Contact
Rose Zhang
1 project • 0 followers
Contact
Siyang Su
1 project • 0 followers
Contact
Donglin Wang
1 project • 0 followers
Contact
lorraine lyu
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.