Code Check (Will it work?)

Hello,

I was wondering if this code was correct and compatible with the movi shield. I checked it using the verify button in ardunio and It said the code was all good. I just wanted to check with you to double check and make sure, because I don’t want to ruin the movi shield.

Thanks.

Here is the code (I used the lightswitch example and added the servo sweep example to it along with the SD card audio file playing example):

/****************************************************************************


  • This basic example shows how to use MOVI™’s API to train the call
  • sign “Arduino” and two sentences. When the sentences are recognized
  • they switch on and off an LED on PIN D13. Many boards have a hardwired
  • LED on board already connected to D13.
  • Circuitry:
  • LED is pin D13 and GND
  • Arduino UNO R3, MEGA2560 R3, or Arduino Leonardo R3.
  • Connect speaker to MOVI.
  • IMPORTANT: Always use external power supply with MOVI.
  • Other Arduino-compatible boards:
  • Consult MOVI’s User Manual before connecting MOVI.
  • If you long-press the button on the MOVI (for a couple seconds),
  • MOVI will revert back to the call sign and sentences trained here.
    */

#include “MOVIShield.h” // Include MOVI library, needs to be before the next #include

#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_PIC32)
#include <SoftwareSerial.h> // This is nice and flexible but only supported on AVR and PIC32 architecture, other boards need to use Serial1
#endif
#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

const int led = 13;
MOVI recognizer(true); // Get a MOVI object, true enables serial monitor interface, rx and tx can be passed as parameters for alternate communication pins on AVR architecture

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(led, OUTPUT); // Make LED port writeable
digitalWrite(led, HIGH); // Blink LED.
delay(1);
digitalWrite(led, LOW);
delay(1);

recognizer.init(); // Initialize MOVI (waits for it to boot)

//*
// Note: training can only be performed in setup().
// The training functions are “lazy” and only do something if there are changes.
// They can be commented out to save memory and startup time once training has been performed.
recognizer.callSign(“Arduino”); // Train callsign Arduino (may take 20 seconds)
recognizer.addSentence(“Let there be light”); // Add sentence 1
recognizer.addSentence(“Go dark”); // Add sentence 2
recognizer.addSentence(“Go Servo”); // Add sentence 3
recognizer.train(); // Train (may take 20seconds)
//*/

// recognizer.setThreshold(5); // uncomment and set to a higher value (valid range 2-95) if you have a problems due to a noisy environment.
}

void loop() // run over and over
{
signed int res=recognizer.poll(); // Get result from MOVI, 0 denotes nothing happened, negative values denote events (see docs)
if (res==1) { // Sentence 1.
digitalWrite(led, HIGH); // Turn on LED
recognizer.say(“and there was light!”); // Speak a sentence
}
if (res==2) { // Sentence 2
digitalWrite(led, LOW); // Turn off LED
}
if (res==3) {
recognizer.play(“rooster.wav”); // Play rooster.wav
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
}

1 Like

Try
#include <MOVIShield.h> // Include MOVI library, needs to be before the next #include
#if defined(ARDUINO_ARCH_AVR) // defined(ARDUINO_ARCH_PIC32)
#include <SoftwareSerial.h> // This is nice and flexible but only supported on AVR and PIC32 architecture, other boards need to use Serial1
#endif
#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

const int led = 13;
MOVI recognizer(true); // Get a MOVI object, true enables serial monitor interface, rx and tx can be passed as parameters for alternate communication pins on AVR architecture

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(led, OUTPUT); // Make LED port writeable
digitalWrite(led, HIGH); // Blink LED.
delay(1);
digitalWrite(led, LOW);
delay(1);

recognizer.init(); // Initialize MOVI (waits for it to boot)

//*
// Note: training can only be performed in setup().
// The training functions are “lazy” and only do something if there are changes.
// They can be commented out to save memory and startup time once training has been performed.
recognizer.callSign(“Arduino”); // Train callsign Arduino (may take 20 seconds)
recognizer.addSentence(“Let there be light”); // Add sentence 1
recognizer.addSentence(“Go dark”); // Add sentence 2
recognizer.addSentence(“Go Servo”); // Add sentence 3
recognizer.train(); // Train (may take 20seconds)
//*/

// recognizer.setThreshold(5); // uncomment and set to a higher value (valid range 2-95) if you have a problems due to a noisy environment.
}

void loop() // run over and over
{
signed int res=recognizer.poll(); // Get result from MOVI, 0 denotes nothing happened, negative values denote events (see docs)
if (res==1) { // Sentence 1.
digitalWrite(led, HIGH); // Turn on LED
recognizer.say(“and there was light!”); // Speak a sentence
}
if (res==2) { // Sentence 2
digitalWrite(led, LOW); // Turn off LED
}
if (res==3) { // Sentence 3
recognizer.play(“rooster.wav”); // Play rooster.wav
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos’
delay(15); // waits 15ms for the servo to reach the position
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

It appears to me you had too many {} parens within res==3 that was stopping the flow of opening and closing the mouth by 1 degree. It did not flow from + to - increments.
Also #include “MOVIShield.h” should be #include <MOVIShield.h>