Hello there,
I am currently trying to successfully write a script for two HC-12 Wireless Serial Port Communication Modules that will move a servo 90 degrees when a pushbutton is pressed. This requires the use of two arduino unos. The first arduino (the sender) is wired up to a button as well as an HC-12 module. The second arduino (the receiver) is hooked up to a servo (with an external 6v source) as well as another HC-12 module. When the button is pressed a wireless signal is sent from one arduino to the other. The problem that I am having is the servo will not stay in a position when I push the button. I want the servo to move to 90 degrees when the button is pressed once and stay in that position. Now when I press the button a second time I want the servo to move to 10 degress and stay in that position.
Could anyone help me out and show me what I am doing wrong?
Here is my code for the receiving arduino:
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial mySerial(2, 3); // RX, TX
int servopin = 9;
Servo servo1;
void setup() {
mySerial.begin(9600);
pinMode(servopin, OUTPUT);
servo1.attach(9);
}
void loop() {
if(mySerial.available() > 1){
int input = mySerial.parseInt();//read serial input and convert to integer (-32,768 to 32,767)
if(input == 1111){//if on code is received
servo1.write(90);
delay(5);
}
if(input == 0000){//if off code is received
servo1.write(10);
delay(5);
}
}
mySerial.flush();//clear the serial buffer for unwanted inputs
delay(20);//delay little for better serial communication
}
Thank you!