XU ZHANG
Published

The Second Mouth

A project to show what's The Second Mouth and how to build it

BeginnerFull instructions provided1.5 hours55
The Second Mouth

Things used in this project

Story

Read more

Code

The Second Mouth

C/C++
#include <Seeed_Arduino_GroveAI.h>
#include"TFT_eSPI.h"
#include <Wire.h>
#include <TFT_eSPI.h>
#include "Seeed_FS.h" 
#include"RawImage.h" 
TFT_eSPI tft;
 
GroveAI ai(Wire);
uint8_t state = 0;

float sound;
float len;

void setup(){
  tft.begin();
  pinMode(WIO_MIC, INPUT);
  pinMode(WIO_BUZZER, OUTPUT);
    if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI)) {
        while (1);
    }
    tft.begin();
    tft.setRotation(3);
    drawImage<uint16_t>("hello.bmp", 0, 0); 
  Serial.begin(115200);
  Wire.begin();
  Serial.begin(115200);
 
  Serial.println("begin");
   if (ai.begin(ALGO_OBJECT_DETECTION, MODEL_EXT_INDEX_1)) // Object detection and pre-trained model 1
  {
    Serial.print("Version: ");
    Serial.println(ai.version());
    Serial.print("ID: ");
    Serial.println( ai.id());
    Serial.print("Algo: ");
    Serial.println( ai.algo());
    Serial.print("Model: ");
    Serial.println(ai.model());
    Serial.print("Confidence: ");
    Serial.println(ai.confidence());
    state = 1;
  }
  else
  {
    Serial.println("Algo begin failed.");
  }

}



void loop(){

  sound = analogRead(WIO_MIC);
  len = ai.get_result_len();
  
  if ((((sound > 600)) && ((len >= 1)))) {
    
    for (int count = 0; count < 1; count++) {
      Serial.println(sound);
      Serial.println(len);
      drawImage<uint16_t>("keep quiet.bmp", 0, 0); 
      tone(WIO_BUZZER,294);
      delay(250);
      noTone(WIO_BUZZER);
      tone(WIO_BUZZER,440);
      delay(250);
      noTone(WIO_BUZZER);
      tone(WIO_BUZZER,294);
      delay(250);
      noTone(WIO_BUZZER);
    }
  } else {
    if ((((sound > 600)) && ((len < 1)))) {
      
      for (int count2 = 0; count2 < 1; count2++) {
        Serial.println(sound);
        Serial.println(len);
        drawImage<uint16_t>("so noisy.bmp", 0, 0); 
        tone(WIO_BUZZER,294);
        delay(250);
        noTone(WIO_BUZZER);
        tone(WIO_BUZZER,440);
        delay(250);
        noTone(WIO_BUZZER);
      }
    } else {
      if ((((sound < 600)) && ((len >= 1)))) {
      
      for (int count2 = 0; count2 < 1; count2++) {
        Serial.println(sound);
        Serial.println(len);
        drawImage<uint16_t>("thanks.bmp", 0, 0); 
       
      }
    } else {
      Serial.println(sound);
      Serial.println(len);
      drawImage<uint16_t>("hello.bmp", 0, 0); 
      
    }
    }
  }

   if (state == 1)
  {
    uint32_t tick = millis();
    if (ai.invoke()) // begin invoke
    {
      while (1) // wait for invoking finished
      {
        CMD_STATE_T ret = ai.state(); 
        if (ret == CMD_STATE_IDLE)
        {
          break;
        }
        delay(20);
      }
 
     uint8_t len = ai.get_result_len(); // receive how many people detect
     if(len)
     {
       int time1 = millis() - tick; 
       Serial.print("Time consuming: ");
       Serial.println(time1);
       Serial.print("Number of people: ");
       Serial.println(len);
       object_detection_t data;       //get data
 
       for (int i = 0; i < len; i++)
       {
          Serial.println("result:detected");
          Serial.print("Detecting and calculating: ");
          Serial.println(i+1);
          ai.get_result(i, (uint8_t*)&data, sizeof(object_detection_t)); //get result
 
          Serial.print("confidence:");
          Serial.print(data.confidence);
          Serial.println();
        }
     }
     else
     {
       Serial.println("No identification");
     }
    }
    else
    {
      delay(1000);
      Serial.println("Invoke Failed.");
    }
  }
  else
  {
    state == 0;
  }

}

Credits

XU ZHANG
0 projects • 0 followers

Comments