I am working on a candy drop machine that has 12 servos, one for each bag of candy to drop. When the dispense button is pressed, I want to have the first servo (servo1) drop. then at the end of the sequence I would use count++ to add 1 to the servo name so the next time the button is pressed the 2nd servo (servo2) would drop and so on until I hit 12, after 12, I want the count to reset to 1 again to start over after I re-load the drops. I'm good with the all the code to make the servos work, but not sure how to make the variable for the servo and to reset the count after 12. Any help would be appreciated. just starting to make my sketch so no examples yet.
This is my 5th year making my halloween candy dispenser and each year it's different. So excited to be using arduino and this community is fantastic!
You will want a global variable that drives the state of the machine, using switch/case control structure. The condition to increment (and then reset) the state of your machine is your button press.
Serial printing is great for debugging, as I'm sure you're aware.
As you can see in the following example, within each state (case 0, case 1 and so on), there is an associated function and within those functions is the instruction to the sketch to increment the state by one until it resets in void four().
Hopefully this sketch gives you a good idea at one way to do what you'd like to do.
Note, state machines are game changers, literally and figuratively. It's a very important concept to understand if you like building game-type things or props because it really allows you to break everything down and structure what the total end goal will look like, while avoiding code that is difficult for you or anyone else to follow down some time in the future.
Here's a great video to highlight exactly that by the popular YouTuber, Neshacker.
And here's the example code my daughter and I wrote to show her the same concept:
/* April 04, 2024
* by Tte
* State Machine - timed leveled events
*/
byte level; // 0-255 possible levels
unsigned long currentTime = 0;
unsigned long lastTimeAround = 0;
void setup() {
Serial.begin(115200);
for (int i = 0; i < 20; i++) {
Serial.println(); // 20 blank lines to clear Serial Monitor of lazy or leftover text
}
Serial.println("millisTimedFunctionsByTte\n");
level = 0;
}
void loop() {
// start free running timer
currentTime = millis();
/* set timer condition to check clock every second to
see what level we're on and perform the associated function */
if (currentTime - lastTimeAround > 1000) {
switch (level) {
case 0:
zero();
break;
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
default:
break;
}
// synch the time marker to the free running clock
lastTimeAround = currentTime;
}
}
void zero() {
Serial.print("\tTte ");
level += 1;
}
void one() {
Serial.print("Loves ");
level += 1;
}
void two() {
Serial.print("Cats ");
level += 1;
}
void three() {
Serial.print("and ");
level += 1;
}
void four() {
Serial.println("Dogs:)");
level = 0;
}
Thanks. The funny thing is that the positions of the servos are calculated in the sketch (which runs in Wokwi). It is the function "GenerateDiagram()" and its output is the circuit for Wokwi.
The Servo library uses a timer that triggers a interrupt routine. In the interrupt routine, all the PWM signals are made. It turns out that a basic Arduino board has no problems with many Servo signals.
Nice work! Your sim is so much fun to play around with. Thanks for sharing it!
Watching it makes me think of Eddie Van Halen playing a killer guitar solo.
@Koepel Thanks for the simulator sketch, it's pretty cool. I was having an issue with Pins 45 &46 on my Mega. I could not get any PWM output on them. How did you solve this problem? I see in the dissuasion you were talking about the timer function, which I know get's used on these pins I think. I've done a basic servo.attach for each servo as I'm just testing to make sure all wiring is correct before I move on to fancier things like arrays.
Pin 45 and 46 are just normal digital pins. There should be no reason for them to behave differently. Could there be something wrong with the hardware of the board ? A solder blob that creates a short circuit or a broken copper trace of the PCB ?