Hi, I'm working on a project but my code writing skill level is zero. I can just barely read a little so I'm having a hard time getting this to work. I have copied and pasted from here and there and I'm at a point where I just don't know how to formulate the language to do what I need it to do. Do I use While? Do I use If statements? Please help!
Right now the servo sort of sweeps back and forth but it seems like its receiving two simultaneous instructions because sometimes it glitches and moves fast. If I activate my relay and hold it on, the servo will finish its cycle then stop at 0º (or maybe its 180º, not sure). The code has a description of what I'm trying to do.
A little background on the project: I have a stereo amplifier with a physical power button and no remote controls. If I leave it on constantly, it consumes a good deal of power, far more than an Arduino. Alexa controls the rest of my stereo through IFTT talking to Logitech Harmony (it was just announced that Amazon Echo now supports Harmony directly so I'll make that change soon to eliminate one step) but the only thing left that is not automatic is control of the amp power. I'm trying to automate that one last bit of the process so it can be entirely voice controlled through Alexa. Sure I could use a smart switch in the wall or something else, but what fun is that?
So I built an IR transmitter and an IR receiver with a relay. I have programmed my Logitech Harmony to understand the IR code and it successfully transmits to the IR receiver and activates the relay to a closed position for a brief period (as long as the IR signal is being received).
Now all I need is a bit of code so that the Arduino can detect the 3v signal sent from itself to an open relay that, when closed, will send the 3v back to a pin on the Arduino which will begin the servo sequence to press the power button on the amp.
/* The goal of this program is to cause a servo to hold a position
until a voltage signal is recognized on a pin of the arduino.
The signal comes via a relay with a power source applied to it.
(remote activation of the relay completes the circuit and sends
"HIGH" signal to the arduino pin).
Once the "HIGH" signal is received, the servo will perform a cycle (even if the
cycle takes longer than the duration of the signal), then return to the
original position and wait for "HIGH" on the pin again.
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int relay = 7; // The relay "on" will be on Pin 7
int myServoPin = 9;
void setup()
{
myservo.attach(myServoPin); // attaches the servo on pin 9 to the servo object
pinMode(relay, INPUT);
}
void loop()
{
delay(2000);
while (digitalRead(relay) == LOW) {
for (pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 90 degrees
{ // in steps of degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 10ms for the servo to reach the position
}
delay(2000);
if (digitalRead(relay) == HIGH) {
pos = 180;
for (pos = 180; pos >= 0; pos -= 1) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 400ms for the servo to reach the position
}
}
}
}
Thanks in advance to anyone who can figure out how to clean that mess up!