Weird Problem with ESP-01+Servo
Posted: Fri Jan 15, 2016 6:14 pm
Hi all,
I'm hoping you could help me fix this problem:
I have a LED on GPIO2, & a Servo signal input wire on GPIO0.
The servo just sweeps back and forth constantly instead of moving from 0 degrees to 100 degrees while RSSI > -80 && rssi < 0, and go to back to 0 degrees when RSSI < -80. It should stay either/or, not sweeping.
Here's my code:
I'm hoping you could help me fix this problem:
I have a LED on GPIO2, & a Servo signal input wire on GPIO0.
The servo just sweeps back and forth constantly instead of moving from 0 degrees to 100 degrees while RSSI > -80 && rssi < 0, and go to back to 0 degrees when RSSI < -80. It should stay either/or, not sweeping.
Here's my code:
Code: Select all
#include <myServo.h>
//Servo Motor on GPIO-0
//GPIO-0 HIGH when running code, LOW when uploading program to module.
//GPIO2 to Opto Negative, then Opto Positive to VCC 3.3v
//Optoisolator switches off while signal strength is weaker than -80DBM.
#include <ESP8266WiFi.h>
#include <SPI.h> //Not sure if needed for serial printing.
#define servopin 0
#include <Ticker.h>
#include "myServo.h"
const char* ssid = "AI-THINKER_F53A7F";
const char* password = "";
Servo servo;
void setup(){
Serial.begin(115200);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
Serial.println("OPTO OFF");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
servo.attach(0);
}
void loop() {
int pos;
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
digitalWrite(2, HIGH);
Serial.println("OPTO OFF");
}
else {
long rssi = WiFi.RSSI();
Serial.print(rssi);
if (rssi > -80 && rssi < 0) {
digitalWrite(2, LOW);
Serial.println("OPTO ON");
for (pos = 0; pos <= 100; pos += 1) // goes from 0 degrees to 100 degrees
{ // in steps of 1 degree
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
if (rssi < -80) {
digitalWrite(2, HIGH);
Serial.println("OPTO OFF");
for (pos = 100; pos >= 0; pos -= 1) // goes from 100 degrees to 0 degrees
{
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
}