0

Parallax Kittybot Alt V2 need help with millis()
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Parallax Kittybot Alt V2 need help with millis()

by Blueshark on Thu Jul 11, 2019 4:28 pm

Hi I need help using the millis() call instead of the default delay() call. When I run the music, it ties the Arduino where it cant do anything else during the music play.

I would like to drive other directions while music playing.





Heres my Arduino code...
Code: Select all | TOGGLE FULL SIZE
/***************************************************
 KittyBot Arduino Example Code
 
 Adapted from example code written by Andy Lindsay at Parallax Inc.
 and Limor Fried/LadyAda at Adafruit Industries by Matthew Matz at
 Parallax Inc.
 ***************************************************/
 

/***************************************************
  This is an example for the Adafruit VS1053 Codec Breakout

  Designed specifically to work with the Adafruit VS1053 Codec Breakout
  ----> https://www.adafruit.com/products/1381

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries. 
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// include SPI, MP3, Servo and SD libraries
#include <SPI.h>             // We will use the hardware SPI pins: CLK (13), MISO (12), MOSI (11)
#include "Adafruit_VS1053.h"
#include <SD.h>
#include <Servo.h>
 
//Set up Servo Objects
Servo servoRight;                           
Servo servoLeft;

// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS      7      // VS1053 chip select pin (output)
#define SHIELD_DCS     6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS         4     // Card chip select pin

// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ           3       // VS1053 Data request, ideally an Interrupt pin

// The pin for the PING))) sensor
#define pingPin        A0

// The pins for the servos
#define servoPinL      8
#define servoPinR      9

// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, ping_inches, ping_cm;

// create shield-example object!
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

//bluetooth setup-----------------------------
#include<SoftwareSerial.h>
SoftwareSerial BT(A2,A1); //TX,RX respectively
String state;
//--------------------------------------------



void setup() {

  //bluetooth setup --------------------
  BT.begin(9600);
  Serial.begin(9600);
 //-------------------------------------

 
  freqOut(2, 2000, 3000);

  // use an unconnected analog input to seed the random number generator
  (analogRead(2));

  // initialise the music player
  musicPlayer.begin(); 
  delay(100);
 
  // initialise the SD card
  SD.begin(CARDCS);
 
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);

  // DREQ is on an interrupt pin (on uno, #2 or #3) so we
  // can do background audio playing - not used in this example
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
 
  servoLeft.attach(servoPinL);                      // Attach left signal to pin 13
  servoRight.attach(servoPinR);                     // Attach right signal to pin 12
 
}

void loop() {
 

 
// bluetooth call-----------------------------
  while(BT.available())
  {
    delay(10);
    char c = BT.read();
    state += c; 
  }

  if(state.length()>0){
    Serial.println(state);

  if(state == "happy")
  {
    musicPlayer.playFullFile("purrs.wav");
    delay(50);
  }

  else if(state == "angry")
  {
   musicPlayer.playFullFile("hiss.wav");
   delay(100);
  }

  else if (state == "calm")
  {
    musicPlayer.playFullFile("meow.wav");
    delay(100);
  }

  else if (state == "cat")
  {
    musicPlayer.playFullFile("NyanCat2.wav");
    delay(100);
  }

  else if (state == "shark")
  {
    musicPlayer.playFullFile("Shark2.wav");
    delay(100);
  }

  else if(state == "forward")
  {
    drive_forward();
    delay(100);
  }

  else if(state == "reverse")
  {
    drive_reverse();
    delay(100);
  }

  else if(state == "stop")
  {
    drive_stop();
    delay(100);
  }

  else if(state == "right")
  {
    drive_turnRight();
    delay(100);
  }

  else if (state == "left")
  {
    drive_turnLeft();
    delay(100);
  }

 

 
  state = "";
 }
}




void drive_reverse() {
  servoLeft.writeMicroseconds(1700);         // 1.7 ms -> counterclockwise
  servoRight.writeMicroseconds(1300);        // 1.3 ms -> clockwise
  return;
}

void drive_forward() {
  servoLeft.writeMicroseconds(1300);         // 1.3 ms -> clockwise
  servoRight.writeMicroseconds(1700);        // 1.7 ms -> counterclockwise
  return;
}

void drive_turnLeft() {
  servoLeft.writeMicroseconds(1300);         // 1.3 ms -> clockwise
  servoRight.writeMicroseconds(1300);        // 1.3 ms -> clockwise
  return;
}

void drive_turnRight() {
  servoLeft.writeMicroseconds(1700);         // 1.7 ms -> counterclockwise
  servoRight.writeMicroseconds(1700);        // 1.7 ms -> counterclockwise
  return;
}

void drive_stop() {
  servoLeft.writeMicroseconds(1500);         // 1.5 ms -> still
  servoRight.writeMicroseconds(1500);        // 1.5 ms -> still
  return;
}

void freqOut(int tonePin, long toneDura, long toneFreq) {  // used to play tone on startup
  pinMode(tonePin, OUTPUT);
  toneFreq = 500000L / toneFreq - 10L;
  long cycles = toneDura * 500L / toneFreq;
  for (long idx; idx <= cycles; idx++) {
    digitalWrite(tonePin, HIGH);
    delayMicroseconds(toneFreq);
    digitalWrite(tonePin, LOW);
    delayMicroseconds(toneFreq);
  }
}



Here's a short video on what it does so far....
https://drive.google.com/file/d/135lvF6E1sPBZUZooK1oEsSER6iNf7-ct/view?usp=sharing


Any help on how to use the millis() call on my code will be greatly appreciated.

Cheers...

Blueshark
 
Posts: 142
Joined: Thu Dec 21, 2017 7:19 pm

Re: Parallax Kittybot Alt V2 need help with millis()

by adafruit_support_bill on Thu Jul 11, 2019 4:36 pm

This tutorial explains the basics: https://www.arduino.cc/en/tutorial/BlinkWithoutDelay

Bald Engineer walks you through it line-by-line here: https://www.baldengineer.com/blink-with ... ained.html

And this is a series on how to extend that basic technique to more sophisticated types of multitasking
https://learn.adafruit.com/multi-taskin ... 1/overview
https://learn.adafruit.com/multi-taskin ... 2/overview
https://learn.adafruit.com/multi-taskin ... 3/overview

adafruit_support_bill
 
Posts: 74650
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.