How to make Servo move for 90 and back from 90

Hey.

i have this code, i'm try to understand where i need to config the serve move 90 degrees
and then Whenever I want again he'll turn back again 90 degrees
Thanks!!

 * Belkin WeMo emulation */
fauxmoESP fauxmo;
Servo myservo;  // create servo object to control a servo

void setup() 
{
   Serial.begin(SERIAL_BAUDRATE);
   //setup and wifi connection
   wifiSetup();
    pinMode(D4, OUTPUT);
    myservo.attach(2);
    beginVal = myservo.read();
   // Device Names for Simulated Wemo switches
   fauxmo.addDevice("door");
   fauxmo.onMessage(callback); 
}

void loop() 
{
  if(resetMe){
    delay(1000);
    Serial.println("reset the servo");
    myservo.write(0);
    resetMe=false;
  }
  fauxmo.handle();
}

/* ---------------------------------------------------------------------------
 Device Callback
 ----------------------------------------------------------------------------*/
void callback(uint8_t device_id, const char * device_name, bool state) 
{
  Serial.print("Device "); Serial.print(device_name); 
  Serial.print(" state: ");
  if (state) 
  {
    Serial.println("ON");
  } 
  else 
  {
    Serial.println("OFF");
  }
  
  //Switching action on detection of device name, useful for adding
  //multiple "devices" to a single ESP unit.
  
  if ( (strcmp(device_name, "door") == 0) ) 
  {
    if (state) 
    {
      int newVal = beginVal+15;
      myservo.write(newVal);
      resetMe=true;
      Serial.println("ok");  
    } else{
      myservo.write(beginVal);
    }
  
  }
  
}

void moveServo(int val){
  Serial.println("moveServo");
    //if you need to scale from a potentiometer to use it with the servo (value between 0 and 180)
    //val = map(val, 0, 1023, 0, 180);     
    myservo.write(val);
}

Have you installed the mind-reader library? How is the Arduino supposed to know when you want it to move back?

Move servo to 90 = myservo.write(90)
Move back to 0 = myservo.write(0).

Where you put those commands depends on when you want them to happen and how you're going to tell the Arduino when you want them to happen. "Whenever I want" sounds like you're going press a switch or send a command or something but you haven't given any details.

Steve

Hello MorganS and slipstick

i use this code.

i just want to use it for a Another project with alexa.

Like On that will move 90 degrees and stop, off that will turn back again 90 degrees.

Thanks for try to help me.
MP

Please post your code here so we don't have to go to another website to help you.

...R

Yes, Sure.

i'm try to understand where i need to add to the servo to move 90 degrees.
on command will move 90 degrees and stop, off command will turn back again 90 degrees.

Thanks for try to help me.

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include <Servo.h>

/* Network credentials */
#define WIFI_SSID "$$$"
#define WIFI_PASS "$$$"

#define SERIAL_BAUDRATE 115200  //this is important to set in the Arduino IDE
bool resetMe=false;
int beginVal=0;

/* Belkin WeMo emulation */
fauxmoESP fauxmo;
Servo myservo;  // create servo object to control a servo

void setup() 
{
   Serial.begin(SERIAL_BAUDRATE);
   //setup and wifi connection
   wifiSetup();
    pinMode(D4, OUTPUT);
    myservo.attach(2);
    beginVal = myservo.read();
   // Device Names for Simulated Wemo switches
   fauxmo.addDevice("finger");
   fauxmo.onMessage(callback); 
}

void loop() 
{
  if(resetMe){
    delay(1000);
    Serial.println("reset the servo");
    myservo.write(0);
    resetMe=false;
  }
  fauxmo.handle();
}

/* ---------------------------------------------------------------------------
 Device Callback
 ----------------------------------------------------------------------------*/
void callback(uint8_t device_id, const char * device_name, bool state) 
{
  Serial.print("Device "); Serial.print(device_name); 
  Serial.print(" state: ");
  if (state) 
  {
    Serial.println("ON");
  } 
  else 
  {
    Serial.println("OFF");
  }
  
  //Switching action on detection of device name, useful for adding
  //multiple "devices" to a single ESP unit.
  
  if ( (strcmp(device_name, "finger") == 0) ) 
  {
    if (state) 
    {
      int newVal = beginVal+15;
      myservo.write(newVal);
      resetMe=true;
      Serial.println("ok");  
    } else{
      myservo.write(beginVal);
    }
  
  }
  
}

void moveServo(int val){
  Serial.println("moveServo");
    //if you need to scale from a potentiometer to use it with the servo (value between 0 and 180)
    //val = map(val, 0, 1023, 0, 180);     
    myservo.write(val);
}

/* -----------------------------------------------------------------------------
 Wifi Setup
 -----------------------------------------------------------------------------*/
void wifiSetup() 
{
   // Set WIFI module to STA mode
   WiFi.mode(WIFI_STA);

   // Connect
   Serial.println ();
   Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
   Serial.println();
   WiFi.begin(WIFI_SSID, WIFI_PASS);

   // Wait
   while (WiFi.status() != WL_CONNECTED) 
   {
      Serial.print(".");
      delay(100);
   }
   Serial.print(" ==> CONNECTED!" );
   Serial.println();

   // Connected!
   Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
   Serial.println();
}

Writing to the servo is in the callback function where varaiable 'state' seems to be true for ON, false for OFF. So try this and see if it does what you are looking for:

    if (state) 
    {
      // int newVal = beginVal+15;
      // myservo.write(newVal);
      myservo.write(90);
      resetMe=true;
      Serial.println("ok");  
    } else{
      // myservo.write(beginVal);
      myservo.write(0);
    }

If that's not what you meant try putting the original lines back but changing beginVal + 15 to beginVal + 90.

Steve

slipstick

after i did whay you say the servo sopt spin.. and did not working.

with this down my servo now just spin and did not stop.

what i miss? i want with alexa on - servo will spin 90 degrees.
alexa off servo spin to 0 from 90 degrees

/**************************
********************************************************
* This example code is to support the project at https://www.iliketomakestuff.com/make-an-alexa-controlled-finger/
* project by Bob Clagett @ I Like To Make Stuff
* http://www.iliketomakestuff.com/
*This example code is built from the library and example code below and is provided WITH NO SUPPORT
* Home Automation with Alexa and NodeMCU
* WeMos smart devices emulation using FAUXMOESP Library
*
* Code based on the great open source lib & example code at: 
* http://tinkerman.cat/emulate-wemo-device-esp8266/
* which is based off of the Python example code by: 
* https://github.com/makermusings/fauxmo
* 
* Also, thanks to Sid for Sid's E Classroom
* https://www.youtube.com/c/SidsEClassroom
* 
* fauxmoESP is a library for ESP8266-based devices that emulates a Belkin WeMo device 
* and thus allows you to control them using this protocol, in particular from 
* Alexa-powered devices like the Amazon Echo or the Dot.
* 
*  To activate a device or a gropu of devices, you should use voice commands, like: 
*  "Computer (or Alexa), turn on Light1" or "..., turn off Light1"
*  "Computer (or Alexa), turn on Living Room" or "..., turn off Living Room"
*  "Computer (or Alexa), turn on All Devices" or "..., turn off All Devices"
*
*  Based on code developed by Marcelo Rovai on 19Aug17
*  Visit his blog: https://MJRoBot.org 
  *********************************************************************************/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include <Servo.h>

/* Network credentials */
#define WIFI_SSID "MI"
#define WIFI_PASS "d50t"

#define SERIAL_BAUDRATE 115200  //this is important to set in the Arduino IDE
bool resetMe=false;
int beginVal=0;

/* Belkin WeMo emulation */
fauxmoESP fauxmo;
Servo myservo;  // create servo object to control a servo

void setup() 
{
   Serial.begin(SERIAL_BAUDRATE);
   //setup and wifi connection
   wifiSetup();
    pinMode(D4, OUTPUT);
    myservo.attach(2);
    beginVal = myservo.read();
   // Device Names for Simulated Wemo switches
   fauxmo.addDevice("door");
   fauxmo.onMessage(callback); 
}

void loop() 
{

  fauxmo.handle();
}

/* ---------------------------------------------------------------------------
 Device Callback
 ----------------------------------------------------------------------------*/
void callback(uint8_t device_id, const char * device_name, bool state) 
{
  Serial.print("Device "); Serial.print(device_name); 
  Serial.print(" state: ");
  if (state) 
  {
    Serial.println("ON");
  } 
  else 
  {
    Serial.println("OFF");
  }
  
  //Switching action on detection of device name, useful for adding
  //multiple "devices" to a single ESP unit.
  
  if ( (strcmp(device_name, "door") == 0) ) 
  {
    if (state) 
    {
      int newVal = 90;
      myservo.write(newVal);
      Serial.println("ok");  
    } else{
      myservo.write(beginVal);
    }
  
  }
  
}

void moveServo(int val){
  Serial.println("moveServo");
    //if you need to scale from a potentiometer to use it with the servo (value between 0 and 180)
    //val = map(val, 0, 1023, 0, 180);     
    myservo.write(val);
}

/* -----------------------------------------------------------------------------
 Wifi Setup
 -----------------------------------------------------------------------------*/
void wifiSetup() 
{
   // Set WIFI module to STA mode
   WiFi.mode(WIFI_STA);

   // Connect
   Serial.println ();
   Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
   Serial.println();
   WiFi.begin(WIFI_SSID, WIFI_PASS);

   // Wait
   while (WiFi.status() != WL_CONNECTED) 
   {
      Serial.print(".");
      delay(100);
   }
   Serial.print(" ==> CONNECTED!" );
   Serial.println();

   // Connected!
   Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
   Serial.println();
}

  beginVal = myservo.read();This does not read the position of the servo, it returns the position that the servo was last commanded to move to. As at this point there servo has not been commanded to move what do you expect it to be ? My advice would be to set beginVal to a fixed value.

Incidentally, what type of servo are you using ?

my servo now just spin and did not stop.

It sounds like it may not be a servo at all. Is it by any chance a continuous rotation "servo" ?

so can you help me and tell me What exactly should be done?

i'm new with this, i want to make the servo open / close the door, and i dont know what i miss here :frowning:

mp3lll:
so can you help me and tell me What exactly should be done?

i'm new with this, i want to make the servo open / close the door, and i dont know what i miss here :frowning:

Does the servo turn all the way around, or does it just turn less than a full turn and stop?

Let's start with the servo. Can you provide a link to exactly which one you have ?

Hey all,

Thanks for try to help me, i use MG995 servo.

i want the servo will unlock / lock my door.
i have all the part it make it, i just need help with the code.

how do i make it to look / unlock the door, the code i add did not working for me, what i miss it?
how can i make the servo to getting around like 10 sec One side to lock the door.
and again 10 sec to the other side to unlock the door.

Any help with the code will be Wonderful, appreciate your help.

If you are using a standard MG995 it can never "just spin and not stop". An MG995 will not turn more than about 140 degrees.

When you use the code from post #6 what exactly does the servo do? For ON. Then for OFF. Remember we can't see what's happening so you need to be very clear.

Also how is the servo powered? That is a fairly powerful servo and it MUST be powered separately from the Arduino.

Steve

Hey,

I've been able to get what i need , now it turns 90 degrees as I should.
But how can i make it to turns back from 90 degrees ? Turn around to the other side, idea?

Thanks!
MP

/* ---------------------------------------------------------------------------
 Device Callback
 ----------------------------------------------------------------------------*/
void callback(uint8_t device_id, const char * device_name, bool state) 
{
  Serial.print("Device "); Serial.print(device_name); 
  Serial.print(" state: ");
  if (state) 
  {
    Serial.println("ON");
  } 
  else 
  {
    Serial.println("OFF");
  }
  
  //Switching action on detection of device name, useful for adding
  //multiple "devices" to a single ESP unit.
  
  if ( (strcmp(device_name, "door") == 0) ) 
  {
        myservo.attach(2);
    if (state) 
    {
      int newVal = 90;  
      myservo.write(newVal);
      myDelay(500); //Increase in time = servo will turn more
      Serial.println("open");  
    } else{
      myservo.write(beginVal);
      myDelay(500); //Increase in time = servo will turn more
      Serial.println("close");  
    }
    
			myservo.detach();
  }
  
}

Fix! work like i want to.. :slight_smile:

Thanks for all try to help.

mp3lll:
Fix! work like i want to.. :slight_smile:

Thanks for all try to help.

It is polite to share the method of how you fixed it. That way future students with the same problem will thank you.

The latest version of fauxmo (3.1) has a variable for value which i believe can be set 0-100 designed for dimmers to my understanding but that could also be used in such a way that if a certain value is set then do so many degrees one way or the other depending on value spoken to alexa. I know this post is over a year old and the OP has not responded but if anyone was looking for a similar solution this gives the possability of 100 possitions or choices from one single device if you can remember what number corisponds to what command lol,
I cant even remember when to use an unsigned long or just a char its so difficult int it!