-->
Page 1 of 1

Weird Problem with ESP-01+Servo

PostPosted: Fri Jan 15, 2016 6:14 pm
by derek bernsen
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:

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
   
  }
  }
  }
  }
 


Re: Weird Problem with ESP-01+Servo

PostPosted: Fri Jan 15, 2016 8:06 pm
by WereCatf
I'm not familiar with servos, but I assume that if you try to turn a servo more than its maximum it'd rotate back to beginning, creating this sweeping back-and-forth. Have you checked your servo's maximum?