/* just the servo mechanism for the treasure chest lid opening and closing,
servo control adapted from Adafruit's "classy solution", thanks, Adafruit!
https://learn.adafruit.com/multi-tasking-the-arduino-part-1/a-classy-solution
Triggered by a PIR proximity/motion sensor
*/
#include <Servo.h>
const int pir = 2;
class Opener
{
Servo servo; // the servo
int increment; // increment to move for each interval
int pos = 0; // current servo position
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
public:
Opener(int interval)
{
updateInterval = interval;
/*changing increment will change how many steps between its two travel ends
it will move at at time. Depending on how the servo is connected to the sensor,
ie: distance from the servo horn, more or less may be needed.
*/
increment = 1;
}
void Attach(int pin) {
servo.attach(pin);
servo.write(15); // avoiding zero helps avoid overdriving servo at zero degree end
}
void Detach(int pin) {
servo.detach();
}
void servoSpeedControl() {
delay(100); // good rate of opening and shutting lid, not too slow or fast
}
void oneWay() {
if ((millis() - lastUpdate) > updateInterval) // time to update
{
lastUpdate = millis();
for (pos = 15; pos <= 175; pos += 1)//default pos += 1
{
servo.write(pos);
servoSpeedControl();
}
}
}
void theOtherWay() {
if ((millis() - lastUpdate) > updateInterval)
for (pos = 175; pos >= 15; pos -= 1)
{
servo.write(pos);
servoSpeedControl();
}
}
void Update() {
Attach(11); // start sending servo pulses down signal wire to Arduino pin 11
oneWay(); // open
delay(10000); // hold
theOtherWay(); // close
Detach(11); // stop sending servo pulses while idle to extend life of servo
}
};
Opener opener1(10); // the rate at which the servo motor moves. Less is faster.
void setup() {
pinMode (pir, INPUT_PULLUP);
Serial.begin(9600);
Serial.println(F("halloweenAutoChestMech"));
delay(1000); // time to read the monitor
}
void loop() {
int pirState = digitalRead(pir);
if (pirState == LOW)//reverse logic since the built in pullup resistor pulls the default voltage UP to 5V
{
Serial.println(F("yep")); // for sensor debugging
opener1.Update();
}
else if (pirState == HIGH) {
Serial.println(F("nope")); // also sensor debugging, comment out once it works right
}
}
Here's a third of it. See the note at the top about Adafruit.
https://learn.adafruit.com/multi-tasking-the-arduino-part-1/a-classy-solution
Add two more instances for each of the other servos. Don't use the Arduino to power servos, use a separate +5V signal.
I will reiterate what the others have said with my personal experience. I have zero education and training in anything to do with electronics or computers. Started in about 2014 and believe me, I'm still amateur, I still make tons of mistakes on every build. Having said that, I have made lots of stuff with Arduino, some very complicated builds that took months effort to get right. Any book on Arduino will tell you that Arduino is not for quitters and I'm not implying you are. There's tons of wisdom to be had in this forum, you'll get to know who the gurus are if you spend a little time (a couple of them are in this thread).
The sketch I supplied is similar to what the Servo library has. Get to know that library, see if you can recode what I posted in the Servo library way since it will be much easier to explain for someone who has little time with a group of new learners.
That Wokwi suggestions seems slick. I just heard about it myself today (thanks, @alto777
)
Here's something else that requires no hardware:
Arduino, as in the IDE, was developed to be relatively easy to use for those who wish to prototype quickly, like students. The inventors adapted the Processing programming language, where the relationship between Processing and Java is akin to the relationship between Arduino and C/C++.
So what? Well, Processing, like Arduino, was designed for artists, students ,relative beginners to allow rapid prototyping, albeit in a PC art type application. Therefore, no external electronics are required and the IDE for Processing is also open source and free as in gratis.
Like Arduino, it has grown to encompass all sorts of other things, so much I can't describe it all here, but you should also check it out:
https://processing.org/
By the way, Processing is why Arduino called its programs "sketches", since Processing is intended to be like an artist's sketchbook, if you will, try something, run it, make a quick change, try again.
Not only that, but Arduino and Processing work play well together. I did all the telemetry for a 5 foot, 300 pound Sentry Robot for Hallowe'en (like a mobile arcade game) in Processing, the physical stuff on the machine controlled by Arduino.
Point is, if you're driven, you will also learn. If you're really driven, you'll learn faster and make more mistakes but that's ok, didn't Edison find like, 1000 ways how not to invent the lightbulb before finding the one that did?