Back to Parent

int buzzerPin = D1; //buzzer to arduino pin 9
int led1Pin   = D6;
int studState;
bool shouldBuzz = false;


void setup()
{
 
  pinMode(buzzerPin, OUTPUT); // Set buzzer - pin 6 as an output
pinMode(led1Pin, OUTPUT);

Particle.subscribe( "teachable-machine" , tmEventHandler);
  Particle.variable( "StudState" , studState);
  Particle.variable( "ShouldBuz" , shouldBuzz);
    
}

void loop()
{
    
    //if there is no stud, play a negative sound
   if (shouldBuzz&&studState==2)
    {
        tone(buzzerPin, 1000); // Send 1KHz sound signal...
        digitalWrite(led1Pin, HIGH);
        delay(1000);
        noTone(buzzerPin);
        digitalWrite(led1Pin, LOW);
        shouldBuzz=false;
        Serial.println("buzzstud");
    }
    //if there is a sound, play a double chirp
   else if (shouldBuzz&&studState==1)
    {
        tone(buzzerPin, 3000); // Send 1KHz sound signal...
        digitalWrite(led1Pin, HIGH);
        delay(500);
        noTone(buzzerPin);
        digitalWrite(led1Pin, LOW);
        tone(buzzerPin, 3000);
        digitalWrite(led1Pin, HIGH);
        delay(500);
        noTone(buzzerPin);
        digitalWrite(led1Pin, LOW);
         Serial.println("buzznostud");
        shouldBuzz=false;
    }
    else if(shouldBuzz&&studState==4)
    {
        digitalWrite(led1Pin, HIGH);
        delay(200);
        digitalWrite(led1Pin, LOW);
        delay(200);
        shouldBuzz=false;
    }
    else if(shouldBuzz&&studState==5)
    {
       tone(buzzerPin, 3000); // Send 1KHz sound signal...
        digitalWrite(led1Pin, HIGH);
        delay(500);
        noTone(buzzerPin);
        digitalWrite(led1Pin, LOW);
        tone(buzzerPin, 3000);
        digitalWrite(led1Pin, HIGH);
        delay(500);
        noTone(buzzerPin);
        digitalWrite(led1Pin, LOW);
         Serial.println("buzznostud");
        shouldBuzz=false;
    }
    
    else if (shouldBuzz&&studState==0)
    {
         
    }
    
   studState=0;
  

}

void tmEventHandler(const char *event, const char *data){
    // if there's an incoming event 
    // store this in the indicator boolean
    // and use it to create movement in the next loop()
    shouldBuzz = true;
    Serial.println(11);
    Serial.println(data);
    if (strcmp(data,"1Stud")==0)
    {
        studState=1;
        Serial.println("halluelh");
    }
    else if (strcmp(data,"2No Stud")==0)
    {
        studState=2;
    }
    else if (strcmp(data,"3Buzzer")==0)
    {
        studState=3;
    }
    else if (strcmp(data,"Hey StudSeer")==0)
    {
        studState=4;
    }
    else if (strcmp(data,"Laugh")==0)
    {
        studState=5;
    }
}
Click to Expand

Content Rating

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

0