BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

I dont't understand button (Try to control the sg90 servo motor through the nodemcu)

We’re working on a project.
Try to control the sg90 servo motor through the nodemcu.
However, there is a problem with button control.
I don’t know the on/off control code. Please help me.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
Servo myservo;
int pos=0;

void setup()
{
// Debug console
pinMode(D4,OUTPUT);
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
//pinMode(servoPin, OUTPUT);
myservo.attach(D4);
}
BLYNK_WRITE(GP1) {
myservo.write(param.asInt());
}

void loop()
{
Blynk.run();
for(pos=1; pos<=180; pos+=1) {
myservo.write(pos);
delay(15);
}
delay(1000);
for(pos=180; pos>0; pos-=1) {
myservo.write(pos);
myservo.write(pos);
delay(15);
}
delay(1000);
}

Turn the servo motor on and off help me

Then, how do I fill out the button control code?
The source code has been modified above.

And yet you still didn’t properly format it as per the Welcome Topic :wink: (so I did it for you).

Reading things like that and the Documentation will show you how to do what you ask about.

Blynk%20-%20FTFC

BLYNK_WRITE(vPin) {  // Use Button/Switch Widget set to a corresponding vPin
  if (param.asInt() == 1) {  // If button state is 1 or HIGH
    // Run START motor code (don't block... use timed functions)
  } else {  // If button state is 0 or LOW (or actually anything else then the prior option)
    // Run STOP motor code
  }
}

Just keep asking the same question and posting random snippets of code isn’t going to get you the answers you need.
Blynk is a powerful development environment, but needs at least some understanding of the C++ coding language.
It has a few quirks, like not tolerating delays and a busy void loop, and a few functions like BLYNK_WRITE aren’t very intuitive, but you’ve been pointed at the documentation that explains all of this.
It doesn’t appear that you’ve followed those links and you’ve currently only spent 8 of reading time since joining the forum.

Learning a programming language is like learning a spoken language - it requires study, effort, experimentation and determination.
Do the research and the experimentation and come back with more appropriate questions and code examples once you’ve put some effort on.

Pete.

1 Like

I am going to button up wemos d1 to blynk.
But I couldn’t control the button.
How do I control buttons?
I am not sure about C language.
When you press the on button, I want to know the control code where the device turns on and the off button turns off.

EDIT - Deleted unformatted code - Moderator

please help me

Dude, starting a new topic isn’t going to get you anywhere (other than having the two topics merged by @GunnerTechTools and getting an earful in the process).
Read the documentation and links that have already been provided, and do the backticks thing when you post code.
Blynk%20-%20FTFC

Pete.

1 Like

I try to switch the servo motor on/off control using the switch button by using Wemos D1.
The servo motor is slow to turn, so I’m not sure if it’s controlled or not.
Help me please.

#define BLYNK_PRINT Serial 
#include <Servo.h>

#include <ESP8266WiFi.h> 
#include <BlynkSimpleEsp8266.h>

Servo myservo;  

int pos = 0;    
boolean btn_state = false;

char auth[] = "2af4bd2dbbb24a1cb6e80c81518cd8a4"; 


char ssid[] = "IPTIME";   
char pass[] = "63184990"; 

BLYNK_WRITE(V1) 
{ 
  int pinValue = param.asInt(); // 

  
 if(pinValue == 1)   btn_state = true; 
  else   btn_state = false;
} 

void setup() 
{ 

  Serial.begin(115200); 
 myservo.attach(2);  
  Blynk.begin(auth, ssid, pass); 
} 

void loop() 
{ 
  Blynk.run(); 
  if(btn_state == true) servo_sweep();
}

void servo_sweep() { 
    for (pos = 0; pos <= 180; pos += 1) { 
      myservo.write(pos);                
      delay(15);                         
    }  
    for (pos = 180; pos >= 0; pos -= 1) { 
      myservo.write(pos);              
      delay(15);                       
    }     
}

Don’t use delays, or other blocking code (like for() loops)… especially in the void loop() or even in sub-functions directly called from the void loop().

Look at other timing options with servos…

Her is a couple I have made…

Where should I use the servo motor angle control code?

Look at the servo control part of the examples I have already shown.

#include <Servo.h> 

Servo servo;      // 서보모터 객체 생성

int motor = 2;    //서보모터 핀번호

int relay_blue = 12;  // 노란색 LED
int relay_red = 13;    // 파란색 LED
 
void setup()
{  
  servo.attach(motor);    //모터 시작
  pinMode(motor,OUTPUT);  //모터 출력
  
  pinMode(relay_blue, OUTPUT);      // 릴레이를 출력으로 설정
  pinMode(relay_red, OUTPUT);        // 릴레이를 출력으로 설정
}

int state = LOW;    // 현재 노란색 LED가 켜졌는지, 파란색 LED가 켜졌는지 결정해 주는 변수

void loop()
{
  if(state == HIGH){  // state가 HIGH일 때, 
    digitalWrite(relay_blue, state);   // 노란색 LED ON
    digitalWrite(relay_red, !state);    // 파란색 LED OFF
    for(int angle = 0; angle < 180; angle++){  // 0도에서 180도로 움직임. 노란색 LED 쪽으로 움직임
      servo.write(angle);                 // 서보모터 동작
      delay(5);                           // 서보모터의 급격한 이동을 막아줌
    }
  }
  else {              // state가 LOW일 때,
    digitalWrite(relay_blue, state);  // 노란색 LED OFF
    digitalWrite(relay_red, !state);   // 파란색 LED ON
    for(int angle = 180; angle >= 0; angle--){  // 180도에서 0도로 움직임. 파란색 LED쪽으로 움직임
      servo.write(angle);               // 서보모터 동작
      delay(5);                         // 서보모터의 급격한 이동을 막아줌
    }
  }
  state = !state;       // 상태 반전

  delay(100);           // 딜레이 500
}

I want to control servo motor and LED through switch button through relay. Please help me. I don’t know how to turn on the red light if the servo motor is turned on and the blue LED turns off.

Hello… I merged all your topics about this servo issue into one…

No need for repeatedly creating new topics if you don’t understand or listen to the previous answers.

I have already provided links to show how you can easily control a servo through Blynk… I suggest you start reading and examining them instead of creating more topics asking for the same help