I'm helping a friend who's making a sculpture with singing mouths. The mouths are controlled with solenoids. I wrote him the following code to control the opening and closing of the mouths with two arrays that contain the timings. It works but because I'm using the 'delay' function, I can only control all the mouths (solenoids) at the same time.
// the setup function runs once when you press reset or power the board
int triggerTime[] = {
700,400,300,400,250,300,760,230,850,200,300,250,200,300}; // length of delay in milliseconds
int mouthPin1 = 8;
int openTime[] = {
400,500,700,400,300,400,300,800,400,700,400,300,400,400}; // length of delay in milliseconds
void setup() {
Serial.begin(9600);
pinMode(mouthPin1, OUTPUT);
}
void loop() {
for (int i = 0; i < (sizeof(triggerTime)/sizeof(int)) - 1; i++) {
delay(triggerTime[i]);
digitalWrite(mouthPin1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(openTime[i]); // wait for a second
digitalWrite(mouthPin1, LOW); // turn the LED off by making the voltage LOW
}
// after running through the arrays, the program will wait between 5 and 15 seconds (randomly)
int CoolingTime = random (5000, 15000);
delay(CoolingTime);
}
To control multiple solenoids with different timings, I was looking at this multi-tasking / OOP solution on adafruit. Trying to adapt this solution, I wrote the following code (I'm not very experienced in functions and not sure if this is the way I can use them with an array)
class Singer
{
// Class Member Variables
// These are initialized at startup
int mouthPin; // the number of the mouth pin
int OnTime[]; // array containing milliseconds of on-time
int OffTime[]; // array containing milliseconds of off-time
int i; // index for array of on- and off-times
// These maintain the current state
int mouthState; // used to set the mouth
unsigned long previousMillis; // will store last time mouth was updated
// Constructor - creates a Singer
// and initializes the member variables and state
public:
Singer(int pin, int on[], int off[])
{
mouthPin = pin;
pinMode(mouthPin, OUTPUT);
OnTime[] = on[];
OffTime[] = off[];
mouthState = LOW;
previousMillis = 0;
i=0;
}
void Update()
{
// check to see if it's time to change the state of the mouth
unsigned long currentMillis = millis();
if((mouthState == HIGH) && (currentMillis - previousMillis >= OnTime[i]))
{
mouthState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(mouthPin, mouthState); // Update the actual mouth
if(i = (sizeof(OnTime)/sizeof(int)) - 1) {
i=0;}
else {i++;}
}
else if ((mouthState == LOW) && (currentMillis - previousMillis >= OffTime[i]))
{
mouthState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(mouthPin, mouthState); // Update the actual mouth
}
}
};
int On1[] = {400,600}; // milliseconds of mouth1 on-time
int Off1[] = {600,400}; // milliseconds of mouth1 off-time
int On2[] = {3000,200}; // milliseconds of mouth2 on-time
int Off2[] = {700,500}; // milliseconds of mouth2 off-time
Singer mouth1(12, On1, Off1);
Singer mouth2(13, On2, Off2);
void setup()
{
Serial.begin(9600);
}
void loop()
{
mouth1.Update();
mouth2.Update();
}
It doesn't work. I get the following error:
Arduino: 1.6.5 (Mac OS X), Board: "Arduino/Genuino Uno"
jabberjaw4_T_class.ino: In constructor 'Singer::Singer(int, int*, int*)':
jabberjaw4_T_class:22: error: expected primary-expression before ']' token
jabberjaw4_T_class:22: error: expected primary-expression before ']' token
jabberjaw4_T_class:23: error: expected primary-expression before ']' token
jabberjaw4_T_class:23: error: expected primary-expression before ']' token
expected primary-expression before ']' token
I suspect this has to do with using the array with the function. Could anyone point me in the right direction? Thanks.