Back to Parent

#include <neopixel.h>
#define PIXEL_PIN D2
#define PIXEL_COUNT 30
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel lightstrip = Adafruit_NeoPixel( PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE );
//Designing for IoT 11/14/2019
//Group: Friend Finder
//Function: Turn on leds and solenoids when buttons are pressed.  Mix Pixel colors when both buttons are on.

int solPin = D0;

int times = 1;
int d1 = 100;
int d2 = 1000;
int buttonPin = D4;
int ledPin = D3;
int OnTime = 0;
bool buttonDown=false;//as a trigger
bool onOrNot=false;//if the person is available
int LastClick=0;
bool shouldDoIt = false;

void setup()
{
    lightstrip.begin();
    lightstrip.show();
    
  Serial.begin( 9600 );

  Particle.variable( "times", &times, INT );
  Particle.variable( "d1", &d1, INT );
  Particle.variable( "d2", &d2, INT );
  Particle.function( "setTimes", setTimes );
  Particle.function( "setD1", setDelayOne );
  Particle.function( "setD2", setDelayTwo );
  Particle.function( "test", testSolenoid );

  pinMode(solPin, OUTPUT);
  pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
  pinMode( ledPin , OUTPUT ); // sets pin as output
 
 OnTime = times*(d1+d2);
 
}

void loop()
{
       // find out if the button is pushed
   // or not by reading from it.
   int buttonState = digitalRead( buttonPin );
    
    
 
  
 // delay( 100 );
  
  
  if( buttonState == LOW )
  {
    // turn the LED On
    digitalWrite( ledPin, HIGH);
    
    if(buttonDown==false){
        testSolenoid("1");
        onOrNot=!onOrNot;
        LastClick=millis();
    uint32_t c = lightstrip.Color( 0, 0, 255 );
    for( int i = 0 ; i < lightstrip.numPixels(); i++ ){
        lightstrip.setPixelColor( i, c );
        lightstrip.show();
       // delay(100);
    }
        
        buttonDown=true;
    }
    
  }else{
    // otherwise
    // turn the LED Off
    digitalWrite( ledPin, LOW);
    buttonDown=false;
    
   
    
}

if(onOrNot){
    digitalWrite( ledPin, HIGH);
     doIt( times, d1, d2 );
     
     
    
}else{
    digitalWrite( ledPin, LOW);
     digitalWrite(solPin, LOW);
     
          uint32_t off = lightstrip.Color( 0, 0, 0 );
    for( int i = 0 ; i < lightstrip.numPixels(); i++ ){
        
        lightstrip.setPixelColor( i, off );
        lightstrip.show();
    //    delay( 100); 
    }
 
     
}
  
}

void doIt( int times, int d1, int d2 ){
    
    
   /* 
  for( int i = 0; i < times; i++ )
  {
    digitalWrite(solPin, HIGH);
    delay( d1 ) ;
    digitalWrite(solPin, LOW);
    delay( d2 );
  }
  */
  
  int now=millis();
  if(now==LastClick+d1){
        digitalWrite(solPin, HIGH);
      
  }else if(now==LastClick+d1+d2){
      
        digitalWrite(solPin, LOW);
        LastClick=now;
  }
  
 
  
  
}


int testSolenoid( String command ){
  shouldDoIt = true;
  return 1;
}

int setTimes( String command ){

  Serial.println( "setTimes: " + command );
  int value = command.toInt();
  if( value < 0 ) return -1;
  if( value > 20 ) return -1;
  times = constrain( value, 0, 20 );
  return 1;

}

int setDelayOne( String command ){

  Serial.println( "setDelayOne: " + command );
  int value = command.toInt();
  if( value < 10 ) return -1;
  if( value > 20000 ) return -1;
  d1 = constrain( value, 0, 20000 );
  return 1;

}

int setDelayTwo( String command ){

  Serial.println( "setDelayTwo: " + command );
  int value = command.toInt();
  if( value < 10 ) return -1;
  if( value > 20000 ) return -1;
  d2 = constrain( value, 0, 20000 );
  return 1;

}
Click to Expand

Content Rating

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

0