Back to Parent

int x_state=1;

int n;

int x;

 

double Selector1 = 0.0;

double Selector2 = 0;

int State1 = 0;

int State2 = 0;

 

//Cards will be read by arduino and get stored here;

 

//LED Pins

 

 

int red1= A0;

int green1= A1;

int red2 =D0;

int green2 = D1;

int red3= A4;

int green3 = A5;

int red4 = A6;

int green4= A7;

 

 

 

int Card1=D0;

int Card2=D1;

int Card3=D2;

 

int ButtonState=0;

int CardNumber=0;

 

char publishString[40];

 

void setup()

{

  // Register a Spark variable here

  Spark.variable("Selector1", &Selector1, DOUBLE);

  Spark.variable("Selector2", &Selector2, DOUBLE);

  Spark.variable("CardDetected", &CardNumber, INT);

 

  Serial.begin(9600);

//Spark.function("SelectorSwitchFunction", "State1");

 

 

  // Connect the temperature sensor to A7 and configure it

  // to be an input

  pinMode(D0, INPUT);

  pinMode(D1, INPUT);

  pinMode(D2, INPUT);

  pinMode(A7, INPUT);

 

 

  pinMode (red1, OUTPUT);

  pinMode (green1, OUTPUT);

  pinMode (red2, OUTPUT);

  pinMode (green2, OUTPUT);

  pinMode (red3, OUTPUT);

  pinMode (green3, OUTPUT);

  pinMode (red4, OUTPUT);

  pinMode (green4, OUTPUT);

}

 

void loop()

{

  int reading1 = 0;

  double voltage1 = 0.0;

 

  // Keep reading the sensor value so when we make an API

  // call to read its value, we have the latest one

  reading1 = analogRead(A3);

 

  // The returned value from the Core is going to be in the range from 0 to 4095

  // Calculate the voltage from the sensor reading

if (reading1 > 0 && reading1 < 1000){  State1 = 1; } // Reading from the selector potentiometer

else if (reading1 > 1000 && reading1 < 2000) { State1 = 2; }

else if (reading1 > 2000 && reading1 < 3000) { State1 = 3; }

else if (reading1 > 3000 ) { State1 = 4 ;}

 

  // Calculate the temperature and update our static variable

  Selector1 = State1;

 

int reading2 = 0;

double voltage2 = 0.0;

 

// Keep reading the sensor value so when we make an API

// call to read its value, we have the latest one

reading2 = analogRead(A2);

 

// The returned value from the Core is going to be in the range from 0 to 4095

// Calculate the voltage from the sensor reading

 

 

if (reading2 > 0 && reading2 < 2000){  State2 = 1; } //Reading from the Button Potentiometer

else if (reading2 > 2000 ) { State2 = 4 ;}

 

 

if(State1==4){

  analogWrite (red1,0);

  analogWrite (red2,0);

  analogWrite (red3,0);

  analogWrite (red4,0);

  analogWrite (green1,255);

  analogWrite (green2,255);

  analogWrite (green3,255);

  analogWrite (green4,255);

} else if(State1==3){

  analogWrite (red1,0);

  analogWrite (red2,0);

  analogWrite (red3,0);

  analogWrite (red4,0);

  analogWrite (green2,0);

  analogWrite (green3,100);

  analogWrite (green4,100);

  analogWrite (green1,0);

} else if(State1==2){

analogWrite (red1,255);

 analogWrite (red2,255);

 analogWrite (red3,255);

 analogWrite (red4,255);

 analogWrite (green1,0);

 analogWrite (green2,0);

 analogWrite (green3,0);

 analogWrite (green4,0);

} else if(State1==1){

  analogWrite (red1,255);

  analogWrite (red2,255);

  analogWrite (red3,255);

  analogWrite (red4,255);

  analogWrite (green1,255);

  analogWrite (green2,255);

  analogWrite (green3,255);

  analogWrite (green4,255);

}

 

  if(State1==1 && x_state>>1){

    n=n+1;

  }

    Serial.print("  State1:");

    Serial.print(State1);

    Serial.print("  x_state:");

    Serial.print(x_state);

    Serial.print("  n:");

    Serial.print(n);

    delay(50);

 

 

    x_state=State1;

 

    x=n%4;

 

 

 

    Serial.print("  x:");

    Serial.println(x);

 

// Calculate the temperature and update our static variable

Selector2 = State2;

if  ( x == 0 ) { Spark.publish("FaizanZafarEventSelectorSwitch1", "0", PUBLIC);}

else if (x == 1) { Spark.publish("FaizanZafarEventSelectorSwitch1", "1", PUBLIC);}

else if (x == 2) { Spark.publish("FaizanZafarEventSelectorSwitch1", "2", PUBLIC);}

else if (x == 3) { Spark.publish("FaizanZafarEventSelectorSwitch1", "3", PUBLIC);}

 

 

//if (Card1==HIGH) { Serial.print("Card 1 detected");  }

//else if (Card2==HIGH) { Serial.print("Card 2 detected");  }

//else if (Card3==HIGH) { Serial.print("Card 3 detected");  }

if (State2 == 4) {

 

Spark.publish("FaizanZafarEvent_GroupMade_Detected", "GroupMembers", PUBLIC);

 

/*if (Card1==HIGH) { CardNumber=1;      */Spark.publish("FaizanZafarEvent_Card1_Detected", "Faizan", PUBLIC);

/*else if (Card2==HIGH) { CardNumber=2;  */Spark.publish("FaizanZafarEvent_Card2_Detected", "MoZhang", PUBLIC);

/*else if (Card3==HIGH) { CardNumber=3;  */Spark.publish("FaizanZafarEvent_Card3_Detected", "Sanika", PUBLIC);

 

int nowTime= millis();

unsigned nowSec = nowTime/1000UL;

        unsigned sec = nowSec%60;

        unsigned min = (nowSec%3600)/60;

        unsigned hours = (nowSec%86400)/3600;

        sprintf(publishString,"%u:%u:%u",hours,min,sec);

        Spark.publish("FaizanZafarEvent_GroupUptime",publishString, PUBLIC);

 

} else if (State2==1) {Serial.print("No Groups Made yet");}

 

 

delay(2000);

 

}
Click to Expand

Content Rating

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

0