Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "config.h"
- #include <Wire.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_ADXL345_U.h>
- #include "CircularEase.h"
- #include "QuadraticEase.h"
- #include "QuinticEase.h"
- #include "BounceEase.h"
- #include "CubicEase.h"
- #include "ExponentialEase.h"
- #include "QuarticEase.h"
- #include "SineEase.h"
- #include "LinearEase.h"
- CircularEase circular;
- QuadraticEase quadratic;
- QuinticEase quintic;
- BounceEase bounce;
- CubicEase cubic;
- ExponentialEase exponential;
- QuarticEase quartic;
- SineEase sine;
- LinearEase linear;
- EasingBase *currentEase;
- enum Ease { CircularIn, CircularOut,
- QuadraticIn, QuadraticOut,
- QuinticIn, QuinticOut,
- BounceIn, BounceOut,
- CubicIn, CubicOut,
- ExponentialIn, ExponentialOut,
- QuarticIn, QuarticOut,
- SineIn, SineOut,
- Linear
- };
- #define RX_WAITING 0
- #define RX_UP 1
- #define RX_DOWN -1
- #define FADE_WAITING 0
- #define FADE_FADE_TOP 1
- #define FADE_FADE_BOTTOM -1
- #define ACCEL_WAITING 0
- #define ACCEL_TRIGGERED 1
- /* --- Button in lieu of RX --- */
- const int rxButton = 2;
- int buttonState;
- int lastButtonState = HIGH;
- unsigned long lastDebounceTime = 0;
- unsigned long debounceDelay = 50;
- /* --- Adafruit.io --- */
- String foot = "l";
- AdafruitIO_Feed *pub;
- AdafruitIO_Feed *sub;
- int rxValue = 0;
- /* --- Accelerometer CONFIG --- */
- const int accelThreshold = 2;
- const int accelDefaultDelay = 200;
- /* --- Accelerometer --- */
- Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
- int accelCheckDelay = accelDefaultDelay;
- int accelPauseDelay = 0;
- float xVal = 0.0;
- unsigned long previousAccelMillis;
- int accelState = ACCEL_WAITING;
- /* --- Vibration --- */
- int fadeState = FADE_WAITING;
- int currentIndex = 0;
- bool isFadeIn = true;
- unsigned long previousFadeMillis;
- /* --- Vibration CONFIG --- */
- int fadeDuration = 400;
- int minPWM = 500;
- int maxPWM = 1023;
- // Specify pins twice, once for in and once for out.
- int pinsTop[] = {
- 16, 16, 15, 15, 13, 13
- };
- int pinsBottom[] = {
- 12, 12, 14, 14
- };
- // Specifiy type of fade for each pin (in and out)
- Ease pinsTopSeq[] = {
- QuinticOut, SineIn, QuinticOut, SineIn, QuinticOut, SineIn
- };
- Ease pinsBottomSeq[] = {
- QuinticOut, SineIn, QuinticOut, SineIn, QuinticOut, SineIn
- };
- void setup() {
- Serial.begin(115200);
- while (!Serial) {
- ;
- }
- // Start connect to io.adafruit.com´
- Serial.print("Connecting to Adafruit IO");
- io.connect();
- if ( foot == "r" ) {
- pub = io.feed("01-user2-r");
- sub = io.feed("01-user1-l");
- } else if (foot == "l" ) {
- pub = io.feed("01-user1-l");
- sub = io.feed("01-user2-r");
- }
- // Message RX handler.
- sub->onMessage(handleMessage);
- // wait for a connection
- while (io.status() < AIO_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- // Connected to io.
- Serial.println(io.statusText());
- // Start acceleromoter
- if (!accel.begin()) {
- /* There was a problem detecting the ADXL345 ... check your connections */
- Serial.println("No ADXL345 detected ... Check wiring.");
- while (1);
- }
- // Set all vibration motor pins to output
- int numPinsT = sizeof(pinsTop) / sizeof(int);
- for (int i = 0; i < numPinsT; i++) {
- pinMode(pinsTop[i], OUTPUT);
- }
- int numPinsB = sizeof(pinsBottom) / sizeof(int);
- for (int i = 0; i < numPinsB; i++) {
- pinMode(pinsBottom[i], OUTPUT);
- }
- pinMode(0, OUTPUT);
- digitalWrite(0, HIGH);
- // Dev - Button pin.
- // pinMode(rxButton, INPUT_PULLUP);
- pub->save(99);
- }
- void loop() {
- checkButton();
- // Adafruit io needs this!
- io.run();
- // Checks if rx value has changed and starts vibration seq.
- rxVibration();
- // Checks if accel movement is above threshold and send event.
- accelerometer();
- delay(10);
- }
- void accelerometer() {
- unsigned long currentMillis = millis();
- int currentDelay = currentMillis - previousAccelMillis;
- float xChange;
- // Accelerometer Readings
- sensors_event_t event;
- accel.getEvent(&event);
- if (accelPauseDelay > 0 && currentDelay >= accelPauseDelay) {
- accelPauseDelay = 0;
- } else if (accelPauseDelay > 0) {
- xVal = event.acceleration.x;
- Serial.println("paused");
- return;
- }
- // ACCELEROMETER reading...every {accelCheckDelay}ms
- if ( currentDelay >= accelCheckDelay ) {
- // How much has x val changed since last loop.
- xChange = event.acceleration.x - xVal;
- if (accelState != ACCEL_WAITING) {
- Serial.println("Accel waiting.");
- pub->save(0);
- accelState = ACCEL_WAITING;
- }
- // If change is greater than threshold.
- if (accelState == ACCEL_WAITING && xChange > accelThreshold) {
- Serial.println("Accel movement down. Sending: 1.");
- pub->save(1);
- }
- // If change is less than threshold
- if (accelState == ACCEL_WAITING && xChange < 0 - accelThreshold) {
- pub->save(-1);
- }
- if ( accelState == ACCEL_WAITING && abs(xChange) > accelThreshold ) {
- accelState = ACCEL_TRIGGERED;
- Serial.println("pause");
- // Increase delay for the next loop. Debouncing.
- accelPauseDelay = 2000;
- }
- xVal = event.acceleration.x;
- /* Display the results (acceleration is measured in m/s^2) */
- Serial.print("X: "); Serial.print(event.acceleration.x); Serial.println(" ");
- // Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" ");
- // Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" "); Serial.println("m/s^2 ");
- previousAccelMillis = currentMillis;
- }
- }
- void rxVibration() {
- double easedPos;
- boolean singleFadeFinished;
- int currentFadeMs;
- if (rxValue == RX_WAITING && fadeState == FADE_WAITING) {
- resetPins();
- }
- // RX_DOWN=FADE_TOP
- if (rxValue == RX_DOWN && fadeState == FADE_WAITING) {
- Serial.println("START SEQ TOP");
- fadeState = FADE_FADE_TOP;
- configureEase(pinsBottomSeq[currentIndex]);
- }
- // RX_UP=FADE_BOTTOM
- if (rxValue == RX_UP && fadeState == FADE_WAITING) {
- Serial.println("START SEQ BOTTOM");
- fadeState = FADE_FADE_BOTTOM;
- configureEase(pinsBottomSeq[currentIndex]);
- }
- // What stage in fade are we at & has it finished?
- // currentFadeMs = millis() - previousFadeMillis
- currentFadeMs = min(int(millis() - previousFadeMillis), fadeDuration);
- singleFadeFinished = (currentFadeMs >= fadeDuration);
- int currentPin;
- Ease currentEaseSeq;
- if ( fadeState == FADE_FADE_TOP ) {
- currentPin = pinsTop[currentIndex];
- currentEaseSeq = pinsTopSeq[currentIndex];
- }
- if ( fadeState == FADE_FADE_BOTTOM ) {
- currentPin = pinsBottom[currentIndex];
- currentEaseSeq = pinsBottomSeq[currentIndex];
- }
- if ( fadeState == FADE_FADE_BOTTOM || fadeState == FADE_FADE_TOP ) {
- easedPos = calculateEase(inOrOut(currentEaseSeq), isFadeIn, currentFadeMs);
- int mappedPos;
- if (isFadeIn == false) {
- mappedPos = map(easedPos, 0, maxPWM, minPWM + 250, maxPWM);
- } else {
- mappedPos = map(easedPos, 0, maxPWM, minPWM, maxPWM);
- }
- analogWrite(currentPin, mappedPos);
- Serial.print("["); Serial.print(currentPin); Serial.print("] "); if (isFadeIn == true) {
- Serial.print(currentFadeMs);
- } else {
- Serial.print(fadeDuration - currentFadeMs);
- }; Serial.print(" - "); Serial.println(mappedPos);
- }
- // Fade has finished. Check whether we're through a full sequence yet.
- if ( fadeState != FADE_WAITING && singleFadeFinished == true) {
- int numPins = 0;
- isFadeIn = !isFadeIn;
- if (fadeState == FADE_FADE_TOP) {
- numPins = sizeof(pinsTop) / sizeof(int);
- } else {
- numPins = sizeof(pinsBottom) / sizeof(int);
- }
- if ( currentIndex >= numPins - 1 ) {
- currentIndex = 0;
- if ( rxValue == RX_WAITING ) {
- fadeState = FADE_WAITING;
- resetPins();
- Serial.println("FADE WAITING");
- }
- } else {
- currentIndex++;
- Serial.println("FINISHED PIN.");
- if (isFadeIn == true) {
- Serial.print("NEXT (in): ");
- } else {
- Serial.print("NEXT (out): ");
- }
- Serial.println(currentIndex);
- }
- if (fadeState == FADE_FADE_TOP) {
- configureEase(pinsTopSeq[currentIndex]);
- } else {
- configureEase(pinsBottomSeq[currentIndex]);
- }
- }
- }
- void resetPins() {
- int numPinsT = sizeof(pinsTop) / sizeof(int);
- for (int i = 0; i < numPinsT; i++) {
- analogWrite(pinsTop[i], 0);
- }
- int numPinsB = sizeof(pinsBottom) / sizeof(int);
- for (int i = 0; i < numPinsB; i++) {
- analogWrite(pinsBottom[i], 0);
- }
- isFadeIn = true;
- }
- double calculateEase(int easeDirection, bool isIn, int t) {
- int _t = (isIn == true) ? t : fadeDuration - t;
- if (easeDirection == 1) {
- return calculateEaseDirection(currentEase, &EasingBase::easeIn, _t);
- } else {
- return calculateEaseDirection(currentEase, &EasingBase::easeOut, _t);
- }
- }
- double calculateEaseDirection(
- EasingBase* easingMethod_, double(EasingBase::*easer_)(NUMBER) const, int t) {
- // Serial.println(t);
- return (easingMethod_->*easer_)(t);
- }
- int inOrOut(Ease ease) {
- switch (ease) {
- case CircularIn:
- case QuadraticIn:
- case QuinticIn:
- case BounceIn:
- case CubicIn:
- case ExponentialIn:
- case QuarticIn:
- case SineIn:
- case Linear:
- return 1;
- break;
- case CircularOut:
- case QuadraticOut:
- case QuinticOut:
- case BounceOut:
- case CubicOut:
- case ExponentialOut:
- case QuarticOut:
- case SineOut:
- return 0;
- break;
- default:
- return 1;
- break;
- }
- }
- void configureEase(Ease ease) {
- switch (ease) {
- case CircularIn:
- case CircularOut:
- Serial.println("circular");
- currentEase = &circular;
- break;
- case QuadraticIn:
- case QuadraticOut:
- Serial.println("quadratic");
- currentEase = &quadratic;
- break;
- case QuinticIn:
- case QuinticOut:
- Serial.println("quintic");
- currentEase = &quintic;
- break;
- case BounceIn:
- case BounceOut:
- Serial.println("bounce");
- currentEase = &bounce;
- break;
- case CubicIn:
- case CubicOut:
- Serial.println("cubic");
- currentEase = &cubic;
- break;
- case ExponentialIn:
- case ExponentialOut:
- Serial.println("Exponential");
- currentEase = &exponential;
- break;
- case QuarticIn:
- case QuarticOut:
- Serial.println("Quartic");
- currentEase = &quartic;
- break;
- case SineIn:
- case SineOut:
- Serial.println("sine");
- currentEase = &sine;
- break;
- case Linear:
- Serial.println("linear");
- currentEase = &linear;
- break;
- default:
- Serial.println("default");
- currentEase = &linear;
- break;
- }
- currentEase->setDuration(fadeDuration);
- currentEase->setTotalChangeInPosition(maxPWM);
- previousFadeMillis = millis();
- }
- void handleMessage(AdafruitIO_Data *data) {
- rxValue = atoi((char *)data->value());
- Serial.print("RX VALUE: ");
- Serial.println(rxValue);
- }
- void checkButton() {
- int reading = digitalRead(rxButton); // HIGH = off // LOW = on
- // If the switch changed, due to noise or pressing:
- if (reading != lastButtonState) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- // whatever the reading is at, it's been there for longer than the debounce
- // delay, so take it as the actual current state:
- // if the button state has changed:
- if (reading != buttonState) {
- buttonState = reading;
- if (buttonState == LOW) {
- if (rxValue == RX_WAITING) {
- rxValue = RX_DOWN;
- Serial.println("RX DOWN");
- } else if (rxValue == RX_DOWN) {
- rxValue = RX_UP;
- Serial.println("RX UP");
- } else {
- rxValue = RX_WAITING;
- Serial.println("RX WAITING");
- }
- }
- }
- }
- lastButtonState = reading;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement