[SOLVED... kinda] Song and Dance

So I’m trying to multistring (and yes, I’ve read the guidance pinned at the top of the page but I haven’t successfully implemented it).

I basically have a motor attached to an IR sensor and WHILE the sensor reads low the motor runs. I need to play a music script at the same time (I’ve found one using a piezo and two basic commands).

So basically, how would I implement this? I’ve given it some thought and really I could dual-process it by wiring up an arduino Nano with the sound code to my Uno and getting the Uno to tell the Nano when to play.
But that doesn’t seem elegant…

Here’s my code:

/***************  INTERACTIVE WINDOW DISPLAY  ***************/

/*  The circuit is placed within a mirror in a shop window  

    It uses an Arduino Uno, L293D motor controller,         

    2 microswitches, low V DC bidirectional motor and an    

    IR distance sensor with digital output                  

                                                            

    This program will check the IR sensor and if it         

    receives a LOW return it checks both the end switches   

    (NO and attached to gnd) before deciding how to move.                       

    Controller pins: Enable, L1, L2                         

    Enable is power override, L1&2 choose direction

    

    L293D was wired with reference to https://learn.adafruit

    .com/downloads/pdf/adafruit-arduino-lesson-15-dc-

    motor-reversing.pdf

    Thanks to johnwasser on the Arduino forums for help with

    endstop debugging                                       

    Thanks to Andy Maybury for help with further debugging  */

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




/******************** GLOBAL CONTROLS ***********************/

//  Pin declarations

int leftTrigger = 7;  

int rightTrigger = 5; 

int IR = 6;

int enable = 11;

int leftSpin = 10;

int rightSpin = 9;




//  Control declarations

int IRval;                  //Value from IR sensor

int lVal;                   //Value from Left trigger

int rVal;                   //Value from Right trigger

boolean drctn;              //Left or right (chip control)

int velocity = 255;         //Motor speed control

int off = 0;                //Easy-read off 

boolean left = true;        //(chip control)

boolean right = false;      //(chip control)

int rnd;                    //Holds random value




/********************* CIRCUIT SETUP ************************/

void setup() {

  //  Declaring pin uses

pinMode(leftTrigger,INPUT_PULLUP);

pinMode(rightTrigger, INPUT_PULLUP);

pinMode(IR, INPUT_PULLUP);

pinMode(leftSpin, OUTPUT);

pinMode(rightSpin, OUTPUT);

pinMode(enable, OUTPUT);




//DEBUG

Serial.begin(9600);

}




void loop(){

/*********************** RANDOMISER  ************************/

//   Uses a larger range for better randomisation. 

  rnd = random(0, 1000);  

  drctn = right;                    //default case

  if (rnd > 500){drctn = left;}   //option 2




/**********************  MAIN CODE **************************/

//First we read the IR state <----------------- HERE IS WHERE I WANT TO RUN MY MUSIC CODE

 while (digitalRead(IR) == LOW){

    if (digitalRead(leftTrigger) == LOW){drctn = right;}

    if (digitalRead(rightTrigger) == LOW){drctn = left;}

    setMotor(velocity, drctn);




    //DEBUG

    Serial.print(rnd);

    Serial.print("  ");

    Serial.print(drctn);

    Serial.print("    ");

  }

setMotor(off,drctn);

}




/**************** motor control function  ******************/

void setMotor(int power, boolean direct){

  analogWrite(enable, power);

  digitalWrite(leftSpin, direct);

  digitalWrite(rightSpin, !direct); 

}

See this pinned thread for how to simulate multitasking/multithreading: http://forum.arduino.cc/index.php?topic=223286.0

johnwasser: See this pinned thread for how to simulate multitasking/multithreading: http://forum.arduino.cc/index.php?topic=223286.0

See above.

SJMaybury: So I'm trying to multistring (and yes, I've read the guidance pinned at the top of the page but I haven't successfully implemented it).

Sorry I missed that. OK, so what went wrong when you tried to implement the multitasking and why is none of that code in your sketch?

Well neither ended up working. I've just decided to delegate the song work to my Arduino Nano, should come out easier.