Servo sequence when pin is HIGH

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!

You need to start at a much lower level and improve your coding skills before tackling a project like this.

The Arduino IDE comes with a bunch of simple examples that teach you how to do most things. Take a day or two to learn those first and the big projects will be much, much easier.

Now all I need is a bit of code so that the Arduino can detect the 3v signal sent from itself

Why would you need to do that? You KNOW when you set the pin high.

PaulS:
Why would you need to do that? You KNOW when you set the pin high.

It's going through a relay. The 3v output goes to the relay which is open, with a return wire connected to pin 9. If the relay is open, pin 9 should have no signal. If the relay is closed, the 3v will now reach pin 9 and activate the sequence. Is there a better way of doing it?

It’s going through a relay.

What is? If the Arduino is, as you stated, turning the relay off and on, there is no reason to test whether it turned the relay on or off.

The 3v output is sent to a relay. The Arduino does not control the relay, the relay is remote controlled. When the relay is activated, the 3v output from the Arduino will essentially connect to pin 9.

What is to happen when pin 9 sees a high?

    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
       if (digitalRead(pin9) == HIGH){
      // do something? pos = 0 to end the for loop?
      // Reset the servo position at the same time? Leave servo where it is?
      }
    }

DrewRacer406:
The 3v output is sent to a relay. The Arduino does not control the relay, the relay is remote controlled. When the relay is activated, the 3v output from the Arduino will essentially connect to pin 9.

Sorry I mean pin 7, not 9. 9 controls the servo.

CrossRoads:
What is to happen when pin 9 sees a high?

    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
      if (digitalRead(pin9) == HIGH){
     // do something? pos = 0 to end the for loop?
     // Reset the servo position at the same time? Leave servo where it is?
     }
   }

When pin 7* (sorry, not 9) sees high, the servo should move to a position, say 90º, then return to its original position and wait for another High signal.

So, put some code in to say that:

    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
       if (digitalRead(pin7) == HIGH){
      myServo.write(90);
      delay(time to allow it to get there);
      myServo.write(0);
       delay(time to allow it to get there);
      }
    }

CrossRoads:
So, put some code in to say that:

Ahhh, the IF goes inside the FOR! I knew I was close, thanks for helping with that. I'll give it a shot at home tonight.

Yes, need to capture the state of the pin somewhere in your code, either as part of the existing servo movement if that's what closes the switch, or independently if the switch can close without the servo moving.
Maybe both even.