-2

My project is that I am building an animated (singing, dancing, head turning, and arm waving) snowman.

I have 3 DC motors for individual functions that are to be run via Arduino Adafruit V1 card programming. Motor #1 will control head turning, motor #2 will control arm waving, and motor #3 will control upper torso dancing. The movements will occur both individually and simultaneously at times throughout the time cycle (2 minutes and 45 seconds).

I need figuring out how to program my card to synchronize the motors to the movements and timeframes. I am an amateur at this and have never coded anything before this project.

The following is a segment of what my coding needs look like:

When the cycle begins the snowman‘s head (motor 1) will turn slowly to the right for two seconds, then turn left for two seconds (as it returns to center), pauses for 3 seconds, and then turns to the left for 2 seconds, then to right for two seconds (returning to center again). (This is the 11 second mark of the 2 minute and 45 second time cycle; and head stays still until at the 21 second mark it turns right for two seconds and then immediately left for two seconds. (Now at the 25 second mark.) It then pauses until the 33 second mark when it will turn again (*subsequent head turn times).

Simultaneously when the cycle begins, the arm (motor 2) begins waving. This motor will spin slowly to the right for 2 seconds, pause for 9 seconds, and turns left for 2 seconds again (to return to starting position). Then motor 2 will continuously turn right for 2 seconds and then left for 2 seconds (for continuous arm waving) until it reaches 33 second mark (of the 2 minute and 45 second time cycle). This motor/arm then pauses until it will turn again (*subsequent arm waving times).

At the 11 second mark (of the 2 minute and 45 second time cycle), motor 3 will turn on continuously to the right for 22 seconds (moving the snowman’s torso in dancing motion). It then pauses at this 33 second mark until it will turn again (*subsequent motor 3/dancing times).

  • At the 33 second mark, the first song ends and a new one subsequently begins, and with this the three motors resume their functions at specific times (similar to above synchronization) that need to be written out/programmed like above.
  • Comments are not for extended discussion; this conversation has been moved to chat. – VE7JRO May 11 at 21:01
-1

You need to re-think what you are trying to do. As Majenko said, forget about delay. Map out a time-line, starting when your program begins. Figure out what you want to happen as time passes. Say for example you want your snowman to act out a sequence for 20 seconds, then repeat that sequence. say for the first 2 seconds you want motor 1 to run, then for the next 5 seconds you want motor 2 and motor 1 to run, then for 3 more seconds you want motor 1 to stop and motors 2 and 3 to run (now you're at 10 seconds). Finally, you want all 3 motors to run for 10 seconds. Then you want the cycle to repeat.

Record the time in millis(). That's your zero. Figure out the amount of elapsed time since the start, and when you get to the desired points on your time-line, trigger different motor states. If you want your snowman's actions to repeat, figure out the repeat time, and write your loop to repeat each time the full repeat time passes.

The basic shell might look something like this

unsigned long start;
unsigned long cycleTime = 20,000; //Say a full cycle time for your snowman is 20 seconds

void setup(); {
  start = millis();
  //more setup code
}


void loop() {
  //Elapsed will contain the number of milliseconds since the program started
  unsigned long elapsed = millis() - start;

  //timeInCycle will count from 0 to 20,000 (20 seconds) and then repeat
  //"%" is the modulo, or remainder, operator
  unsigned long timeInCycle = elapsed %  cycleTime; 

  //use timeInCycle to decide when to trigger the different actions of 
  //your snowman for each cycle
}


I'm not going to code your sequence for you. That's up to you. Your code is impossible to read without any indentation, so I won't try to decipher it.

| improve this answer | |
  • Comments are not for extended discussion; this conversation has been moved to chat. – VE7JRO May 11 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.