Back to Parent

Code for Terminals (Fans' Cool Balls)
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// This #include statement was automatically added by the Particle IDE.
#include <SparkFunMMA8452Q.h>
// This #include statement was automatically added by the Particle IDE.
#include <SparkFunMMA8452Q.h>
#include <Wire.h> // Must include Wire library for I2C
#include <math.h>
// This #include statement was automatically added by the Particle IDE.

MMA8452Q accel;

//https://learn.sparkfun.com/tutorials/lis3dh-hookup-guide?_ga=2.109610374.2021355271.1575496274-1654527264.1575496274
//http://integratedinnovation.xsead.cmu.edu/gallery/projects/cohort-culture-a3573993-f4c3-4cce-9f96-e8e38a545e59
//https://learn.adafruit.com/rgb-led-strips/arduino-code
//https://www.precisionmicrodrives.com/content/how-to-drive-a-vibration-motor-with-arduino-and-genuino/

#define LED_PIN   A2
#define LED_COUNT 30
#define LED_TYPE WS2812B
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, LED_TYPE); //type
uint32_t c = strip.Color(255,0,0);

int fsrPin = A5;
int accelPin = D6;
int vibPin = D5;

int bettime=0;
int scoretime=0;

int fsrRead=0;
int betcorrect=0;

int X[10];
int Y[10];
int Z[10];
int violence = 0;

int n=0;
//monitor
int accx=0;
int accy=0;
int accz=0;
/*
// control publish interval
long lastPublishedAt = 0;
int publishAfter = 1000;//delay
*/

void setup()
{
    pinMode(fsrPin, INPUT);
    pinMode(accelPin, INPUT);
    pinMode(vibPin, OUTPUT);
    
    //accel.init();
    accel.begin(SCALE_2G, ODR_1);

    Particle.subscribe(  "coolball/homescore" , homescore );
    
    //Particle.variable("accx", &accx, INT);
    //Particle.variable("accy", &accy, INT);
    //Particle.variable("accz", &accz, INT);
    Particle.variable("fsrRead", &fsrRead, INT);
    Particle.variable("violence", &violence, INT);
    Particle.variable("betcorrect", &betcorrect, INT);
    Particle.variable("bettime", &bettime, INT);
    
    digitalWrite(vibPin, LOW);
}

void loop()
{
    fsrRead=analogRead(fsrPin);
    if (fsrRead>1200)
    {
        if (millis()-bettime > 3000)
        {
            bettime=millis();//renew bet time
        }
    }

    bright();
    //digitalWrite(vibPin, LOW);
    
}


void bright()
{
    if (accel.available()){
    accel.read(); // Update accelerometer data

    //test range
    accx=accel.x;
    accy=accel.y;
    accz=accel.z;

    X[n%10]=accel.x;
    Y[n%10]=accel.y;
    Z[n%10]=accel.z;
    
    //variation of the acceleration values
    int varX = 0;
    int varY = 0;
    int varZ = 0;
    //first, compute the average
    int avgX = 0;
    int avgY = 0;
    int avgZ = 0;
    for (int i=0; i<10; i++)
    {
        avgX=avgX+X[i];
        avgY=avgY+Y[i];
        avgZ=avgZ+Z[i];
    }
    avgX = avgX/10;
    avgY = avgY/10;
    avgZ = avgZ/10;
    
    //the variation
    for (int i=0; i<10; i++)
    {
        varX=varX+(X[i]-avgX)*(X[i]-avgX);
        varY=varY+(Y[i]-avgY)*(Y[i]-avgY);
        varZ=varZ+(Z[i]-avgZ)*(Z[i]-avgZ);
    }
    /*
    //get the max variance as violence
    if (varX>varY)
    {
        if (varX>varZ) violence=varX;
        else violence=varZ;
    }
    else
    {
        if (varY>varZ) violence=varY;
        else violence=varZ;
    }
    */
    violence = (varX + varY + varZ)/3;
    if (violence> 2800000)  //check the threshold
    {
        int brightness = map(violence, 3000000, 3300000, 60, 255);
        for (int i=0; i<LED_COUNT; i++)
        {
            c = strip.Color(0,0,brightness);
            strip.setPixelColor(i,c);
        }
        strip.show();
        delay(1000);
    }
    else {
        for (int i=0; i<LED_COUNT; i++)
        {
            c = strip.Color(0,0,20);
            strip.setPixelColor(i,c);
        }
        strip.show();
    }
    
    }
}


void homescore(const char *event, const char *data)
{
    scoretime=millis();
    
    // make sure it comes from other device
    String eventName = String( event ); 
    String deviceID = System.deviceID();
    if( eventName.indexOf( deviceID ) != -1 ){
      return;
    }
    else // rainbow color
    {
        rainbow();
    }
    
    //should get event publish time and give to live bet.
    //currently just use the time argon receives the event
    livebet();
}


void livebet ()
{
    //judge if within timeframe
    if (scoretime - bettime < 3000)
    {
        for (int i=0;i<2;i++) //vibrate twice
        {
            digitalWrite(vibPin, HIGH);
            delay(500);
            digitalWrite(vibPin, LOW);
            delay(100);
        }
        betcorrect++;
    }
}

void rainbow()
{
    //start with red
    int r=255;
    int g=0;
    int b=0;
    int i=0;
    //running red
    for (i=0; i<LED_COUNT; i++)
    {
        c = strip.Color(r,g,b);
        strip.setPixelColor(i,c);
        strip.show();
        delay(10);
    }
    //reset to rainbow
    for (i=0; i<5; i++)
    {
        c = strip.Color(127,0,0);
        strip.setPixelColor(i,c);
    }
    for (i=5; i<10; i++)
    {
        c = strip.Color(0,255,0);
        strip.setPixelColor(i,c);
    }
    for (i=10; i<15; i++)
    {
        c = strip.Color(255,255,0);
        strip.setPixelColor(i,c);
    }
    for (i=15; i<20; i++)
    {
        c = strip.Color(255,0,255);
        strip.setPixelColor(i,c);
    }
    for (i=20; i<25; i++)
    {
        c = strip.Color(0,255,0);
        strip.setPixelColor(i,c);
    }
    for (i=25; i<30; i++)
    {
        c = strip.Color(127,255,127);
        strip.setPixelColor(i,c);
    }
    strip.show();
    delay(100);
    //running red (x2)
    for (i=0; i<LED_COUNT; i++)
    {
        c = strip.Color(r,g,b);
        strip.setPixelColor(i,c);
        strip.show();
        delay(10);
    }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0