Back to Parent

//Code for Device 1
#include "Particle.h"
#include "neopixel.h"

void rainbow(uint8_t wait);
SYSTEM_MODE(AUTOMATIC);

#define touchsensor A0
#define motor A3
#define button D0

#define PIXEL_COUNT 1
#define PIXEL_PIN A1
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int force; // 0-4095
int startTime, stopTime;
int desiredTime;

void setup() {
  Particle.subscribe("pixel1", pixelcontrol1, "DEVICE_ID");
  Particle.subscribe("motor1", motorcontrol1, "DEVICE_ID");
  Particle.subscribe("motorshort1", motorvibrate1, "DEVICE_ID");
  strip.begin();
  strip.show();

  startTime=0;
  stopTime=0;
  desiredTime=40;

  pinMode(A0, INPUT);
  pinMode(A3, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop() {
  int buttonState = digitalRead(button);
  if(buttonState == 0) { //pushed
    Particle.publish("pixel2");
    delay(1000);
    /*rainbow(20);*/
  }else{
    strip.setPixelColor(0,  strip.Color(0, 0, 0));
    strip.show();
  }

  force = analogRead(touchsensor);

  Serial.println(force);
  if(force>3000) {
    Particle.publish("motor2");
    delay(1000);
  }else if(force>80 && force<3000) {
    Particle.publish("motorshort2");
    delay(1000);
  }else{
    digitalWrite(motor, LOW);}
    delay(100);
}

void motorcontrol1(const char *event, const char *data){
  digitalWrite(motor, HIGH);
  delay(150);
}

void motorvibrate1(const char *event, const char *data){
  digitalWrite(motor, HIGH);
  delay(50);
}

void pixelcontrol1(const char *event, const char *data){
    rainbow(20);
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(20);
  }
}

uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}
Click to Expand

Content Rating

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

0