Hi folks -
This robotic sculpture - 11 feet long - cycles randomly through programmed moves stored in arrays.
The 6 actuators go HIGH at different times relative to each other using a time offset variable. This allows you to save combinations of movements - like a bottom section moving 800ms before the top, etc. Snake-like movement is possible this way.
These starting time offsets are the way to animate movements between the 6 sections since speed control is not yet possible here.
An example movement where first the middle section moves, then the bottom and the top:
move1[] {800, 0, 1000}
There are two axes ( A and B ), each running the above independently. Each loop is about 30 secs and then another of these preset moves[] are selected.
...and that's the part I can't quite get.
- I'm not able to cleanly pass those array values to the constructor, and
- Not able to get random() to reinit with each loop.
Thanks very much in advance for recommendations, happy to redo the whole thing:
#include <elapsedMillis.h>
// Pins
int
pinsA[] = {3, 11, 6},
pinsB[] = {5, 10, 9};
// Durations. These are constants.
int
durationON = 2000,
durationFADE1 = durationON * .1,
durationFADE2 = durationON * .1,
durationOFF = durationON * 5;
bool serialPrintON = true;
// The animation delays between sections. Each loop, three of these are supposed to be chosen randomly by r below;
unsigned long r = random(0,3);
// 3 vars to pass to the Constructors. They're chosen by r above.
long offsetArray[5][3] = {
{1000, 1000, 1000}, // Move 1
{1200, 600, 1200}, // Move 2
{0, 600, 1200}, // Move 3
{0, 200, 400}, // Move 4
{500, 200, 0}, // Move 5
};
// For some reason only the second array group above ever gets chosen, none of the others.
long offsetA1 = (offsetArray[r][0]);
long offsetA2 = (offsetArray[r][1]);
long offsetA3 = (offsetArray[r][2]);
long offsetB1 = (offsetArray[r][0]);
long offsetB2 = (offsetArray[r][1]);
long offsetB3 = (offsetArray[r][2]);
// Power - these are constants.
int
powerON = 255,
powerFADE1 = powerON * .7,
powerFADE2 = powerON * .3,
powerOFF = 0;
// Duration
int
duration = durationON,
fade1 = durationFADE1,
fade2 = durationFADE2,
fadepower1 = powerFADE1,
fadepower2 = powerFADE2,
interval = durationOFF;
class Constructor
{
int
sectionPin,
sectionPinPower;
long
OnTime,
FadeTime1,
FadeTime2,
FadePower1,
FadePower2,
OffTime,
OffsetTime;
unsigned long previousMillis;
public:
Constructor(int pin, long on, long fade1, long fade2, long fadepower1, long fadepower2, long off, long offset)
{
sectionPin = pin;
pinMode(sectionPin, OUTPUT);
// Durations
OnTime = on;
FadeTime1 = fade1;
FadeTime2 = fade2;
OffTime = off;
OffsetTime = offset;
//Power
FadePower1 = fadepower1;
FadePower2 = fadepower2;
sectionPinPower = 0;
previousMillis = 0;
}
void Update() {
unsigned long currentMillis = millis();
if ((sectionPinPower == powerON) && ((currentMillis - previousMillis) >= (OnTime + OffsetTime)))
{
sectionPinPower = FadePower1; // Turn to Fade 1
previousMillis = currentMillis;
analogWrite(sectionPin, sectionPinPower);
if (serialPrintON) {
printSerial(previousMillis, sectionPin, sectionPinPower, OnTime, FadeTime1, FadeTime2, FadePower1, FadePower2, OffTime, OffsetTime) ;
}
}
else if ((sectionPinPower == FadePower1) && ((currentMillis - previousMillis) >= (FadeTime1 + OffsetTime)))
{
sectionPinPower = FadePower2; // turn it to Fade 2
previousMillis = currentMillis;
analogWrite(sectionPin, sectionPinPower);
if (serialPrintON) {
printSerial(previousMillis, sectionPin, sectionPinPower, OnTime, FadeTime1, FadeTime2, FadePower1, FadePower2, OffTime, OffsetTime) ;
}
}
else if ((sectionPinPower == FadePower2) && ((currentMillis - previousMillis) >= (FadeTime2 + OffsetTime)))
{
sectionPinPower = powerOFF; // turn it off
previousMillis = currentMillis;
analogWrite(sectionPin, sectionPinPower);
if (serialPrintON) {
printSerial(previousMillis, sectionPin, sectionPinPower, OnTime, FadeTime1, FadeTime2, FadePower1, FadePower2, OffTime, OffsetTime) ;
}
}
else if ((sectionPinPower == powerOFF) && ((currentMillis - previousMillis) >= (OffTime + OffsetTime)))
{
sectionPinPower = powerON; // turn it on
previousMillis = currentMillis;
analogWrite(sectionPin, sectionPinPower);
if (serialPrintON) {
printSerial(previousMillis, sectionPin, sectionPinPower, OnTime, FadeTime1, FadeTime2, FadePower1, FadePower2, OffTime, OffsetTime) ;
}
}
}
};
Constructor sectionA1(pinsA[0], duration, fade1, fade2, fadepower1, fadepower2, interval, offsetA1);
Constructor sectionA2(pinsA[1], duration, fade1, fade2, fadepower1, fadepower2, interval, offsetA2);
Constructor sectionA3(pinsA[2], duration, fade1, fade2, fadepower1, fadepower2, interval, offsetA3);
Constructor sectionB1(pinsB[0], duration, fade1, fade2, fadepower1, fadepower2, interval, offsetB1);
Constructor sectionB2(pinsB[1], duration, fade1, fade2, fadepower1, fadepower2, interval, offsetB2);
Constructor sectionB3(pinsB[2], duration, fade1, fade2, fadepower1, fadepower2, interval, offsetB3);
void setup()
{
Serial.begin(115200);
randomSeed(analogRead(0));
}
void loop()
{
sectionA1.Update();
sectionA2.Update();
sectionA3.Update();
sectionB1.Update();
sectionB2.Update();
sectionB3.Update();
}