Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int ledPinRed = 11;
- const int ledPinGreen = 10;
- const int ledPinBlue = 9;
- const int flexPin = A0;
- int pastValue = 0;
- int openMillis = 0;
- int closedMillis = 0;
- // how long will the gesture have to run in millis to register as a gesture?
- const int lengthThreshold = 100;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(ledPinRed, OUTPUT);
- pinMode(ledPinGreen, OUTPUT);
- pinMode(ledPinBlue, OUTPUT);
- }
- void loop() {
- int mapped = readDegrees();
- // if the hand is in a fist
- bool isClosed = mapped > 120;
- // save the current value
- bool currentValue = mapped;
- if (isClosed == true) {
- // the closed fist mode overrides any other mode
- tunesMode();
- } else if (currentValue > pastValue) {
- // increment counter for hand closing
- closedMillis++;
- // if the hand was opening, reset that counter to reset detection
- if (openMillis > 0) {
- openMillis = 0;
- }
- // turn on warming mode if hand has been closing for time threshold
- if (closedMillis > lengthThreshold) {
- warmingMode();
- }
- } else {
- // increment counter for hand opening
- openMillis++;
- // if the hand was closing, reset that counter to reset detection
- if (closedMillis > 0) {
- closedMillis = 0;
- }
- // turn on messaging mode if hand has been closing for time threshold
- if (openMillis > lengthThreshold) {
- messagingMode();
- }
- }
- // set the past value to compare with the current state on next loop
- pastValue = mapped;
- // delay to ensure accurate sensor readings
- delay(1);
- }
- int readDegrees() {
- int flexValue = analogRead(flexPin);
- // convert the value to one that is relative to 0 and positive
- int normalized = abs(flexValue - 76);
- // convert the range to degrees (must be recalibrated according to resistor strength)
- return map(normalized, 0, 35, 0, 90);
- }
- void tunesMode() {
- // tunes mode is yellow
- //Serial.println("Tunes");
- analogWrite(ledPinRed, 255);
- analogWrite(ledPinBlue, 0);
- analogWrite(ledPinGreen, 0);
- }
- void messagingMode() {
- // message mode is purple
- //Serial.println("Messaging");
- analogWrite(ledPinRed, 0);
- analogWrite(ledPinBlue, 0);
- analogWrite(ledPinGreen, 255);
- }
- void warmingMode() {
- // warming mode is blue
- //Serial.println("Warming");
- analogWrite(ledPinRed, 0);
- analogWrite(ledPinBlue, 255);
- analogWrite(ledPinGreen, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment