Project Overview:
I've created a toy elevator for my kid to put her small toy animals in which uses an IR receiver to activate the modified servo which turns a makeshift drum(string attached) and raises/lowers the elevator car. I use two IR remote buttons to activate the servo, one that turns the drum clockwise and the other counter clockwise. I use the delay( ) function to determine how long the servo is active which in turn(pun intended) controls how far the elevator car rises and falls.
Project Problem: Upon being sent the signal from the nano, the servo will spin for the designated time period and then visibly stops only to start spinning again as if I set the command again. This occurs bidirectionally. I can stop the servo by clicking an unregistered IR remote button which interrupts the motor after it finishes it's full spin cycle. The problem did also occur with an older knock-off Arduino Nano I used previously but only occasionally, otherwise it functioned as intended. With my new nano it seems to only exhibit the unintended behavior as described.
Thoughts: I believe the problem deals with my programming and the way it's being interpreted by the controller. I suppose the old nano simply happened to interprete things in a more intended manner than my newer one, but either way the flaw is present in both cases. Note that the servo continues it's cycle indefinitely, which I believe implies that it's not just activating the amount of times the remote happens to send the signal(which it often sends 2-5 repeat signals from one button press based on my IR decoder code).
My goal: I want the servo to turn clockwise/counter clockwise for the set duration of the cycle then stop to await for further IR remote signals.
Though I've worked with various Arduino and ESP boards for a few years now, I would still consider myself primarily a beginner to Ardunio. So please keep this in mind when explaining things to me, though I am willing to put in the work to understand anything said that I'm unfamiliar with.
Project: Toy Elevator
Components: Elegoo Arduino Nano V3.0+ ; Generic Modified SG90 Micro-servo; Elegoo IR Receiver Module; IR TV Remote;
Board Product Link: https://www.elegoo.com/products/elegoo-upgraded-arduino-nano-v3-0-plus
//360 Microservo Toy Elevator Project
#include <Servo.h>
#include <IRremote.h>
Servo myservo;
int IRpin = 7;
IRrecv irrecv(IRpin);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop()
{
if (irrecv.decode(&results))
{
irrecv.resume();
}
if (results.value == 3494706272)
{
myservo.attach(9);
myservo.write(180);
delay(2000);
myservo.detach();
delay(500);
return;
}
else if (results.value == 1641747682)
{
myservo.attach(9);
myservo.write(-180);
delay(2000);
myservo.detach();
delay(500);
return;
}
else return;
}