How to control a servo motor for example with a conditional statement on arduino

Thread Starter

d3rdlegend

Joined Jun 20, 2022
6
Hi im currently trying to attach a servo motor which is used to actuate a spray that I will attach but im currently stuck on how I can make the servo motor's output dependent on the on or off state of the vibration sensor that I have attached. The vibration sensor is set to countdown for 24hrs before trigerring the servo motor to actuate the spray if the reset button or off switch is pushed but what basically happens is that the servo motor is not actually applying the conditions that I have put for it to work. This is the code. I think there is something wrong with my servo commands so im not quite sure as im not quite familiar to the commands yet any input or feedback is much appriciated to direct me on the right track thanks ;)

C-like:
//declaration of variables

#include <Servo.h> //servo library

Servo servo1;

int servoPin = 5;

int pos = 0;

int vibr = 3;

int green = 12;

int red = 11;

int buzzer = 4;

int pb1 = 5;

int buttonState = 0;



void setup() {

servo1.attach(servoPin); // sets the servo motor PIN

pinMode(vibr,INPUT);

pinMode(green,OUTPUT);

pinMode(red,OUTPUT);

pinMode(pb1, INPUT_PULLUP);

}


void loop() {


  int val;

  val=digitalRead(vibr);

  if(val==1)

  {

    digitalWrite(green,HIGH);

    delay(0);

    digitalWrite(red,HIGH);

    delay(2000); // both green and red for 24 hours represent the "danger timing".


    bool value = digitalRead(vibr);

    if (value == 0) {

    servo1.write(180);

    delay(1000);


    servo1.detach();

    delay (3000);


    servo1.attach (servoPin);

    servo1.write(0);

    delay (1000);

}

    delay(1000);


    pinMode(buzzer,OUTPUT);

    delay(0);

    tone(buzzer, 1000);

    delay(1000);        // for 1 sec

    noTone(buzzer);     // Stop sound...

    delay(1000);        // for 1sec

  }


  else

  digitalWrite(red,HIGH);

  digitalWrite(green,LOW);


}
 

ericgibbs

Joined Jan 29, 2010
15,726
hi d3,
Welcome to AAC.
Downloaded your sketch, will give it a run.
E
BTW: Use the code tags for your sketch, use C-like as an Insert.
 

ericgibbs

Joined Jan 29, 2010
15,726
hi d3,
Your Sketch has problems..
Try this simple servo sketch to check out the operation of the servo.
Sweeps CW and the CCW

E
C++:
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(5);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}
 

Thread Starter

d3rdlegend

Joined Jun 20, 2022
6
hi d3,
Your Sketch has problems..
Try this simple servo sketch to check out the operation of the servo.
Sweeps CW and the CCW

E
C++:
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(5);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}
hi d3,
Your Sketch has problems..
Try this simple servo sketch to check out the operation of the servo.
Sweeps CW and the CCW

E
C++:
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(5);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}
Thanks:) will give it a try
 

ericgibbs

Joined Jan 29, 2010
15,726
hi d3,
Modified the Sketch.
Using the vibr input pin to control the sweep of the Servo.

To give the actual control for the project, I need more information on the aerosol actuation times etc

Once you have the basic actions working, you can add the LED's delay etc.
E
BTW: the Serial port on the IDE can be useful for debugging your programs

C-like:
// Servo 2 20/06/22
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position
int vibr = 3;

void setup() {
  Serial.begin(115200);
  Serial.println("Ready");
  myservo.attach(5);  // attaches the servo on pin 5
  pinMode(vibr,INPUT);
}

void loop() {
  bool value=digitalRead(vibr);

/* Serial.println(value); // test ONLY
   If value= 0 then Servo drives to CW and stops
   If value =1 momentary, then Servo drives CCW then CCW once only
   If value =1 premanently, then Servo drives CCW then CCW continously
*/

     if (value == 1) {
      Serial.println("Drive Servo to 180");
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              
    delay(15);                      
  }
     }
   
    if (value == 1) {
    Serial.println("Drive Servo to 0");
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);            
    delay(15);                    
}}
}
 

Thread Starter

d3rdlegend

Joined Jun 20, 2022
6
hi d3,
Modified the Sketch.
Using the vibr input pin to control the sweep of the Servo.

To give the actual control for the project, I need more information on the aerosol actuation times etc

Once you have the basic actions working, you can add the LED's delay etc.
E
BTW: the Serial port on the IDE can be useful for debugging your programs

C-like:
// Servo 2 20/06/22
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position
int vibr = 3;

void setup() {
  Serial.begin(115200);
  Serial.println("Ready");
  myservo.attach(5);  // attaches the servo on pin 5
  pinMode(vibr,INPUT);
}

void loop() {
  bool value=digitalRead(vibr);

/* Serial.println(value); // test ONLY
   If value= 0 then Servo drives to CW and stops
   If value =1 momentary, then Servo drives CCW then CCW once only
   If value =1 premanently, then Servo drives CCW then CCW continously
*/

     if (value == 1) {
      Serial.println("Drive Servo to 180");
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);            
    delay(15);                    
  }
     }
 
    if (value == 1) {
    Serial.println("Drive Servo to 0");
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);          
    delay(15);                  
}}
}[/CODE
So i have applied this but im getting errors this what the code turned out to be..attached file is the project im working on what I just basically want it to do is read the vibration sensor to actuate the servo...... the buzzer is set to alarm after 24hrs(2secs for now for testing) if the vibration sensor is not turned off or reseted and that would cause the servo motor to actuate a sweep or I would maybe make it hold the postion from 0 to 180.
Im new to this field and in programming in general.. I worked on simple stuff like leds and just basic things but I haven't worked w/ servos so I really appriciate the help on setting me on the right direction cheers mate :)
C-like:
// Servo  21/06/22
#include <Servo.h> //servo library
Servo servo1; 
int servoPin = 5; 
int pos = 0; 
int vibr = 3;
int green = 12;
int red = 11;
int buzzer = 4;
int buttonState = 0;
int value = 0;


void setup() {
servo1.attach(servoPin); // sets the servo motor PIN
pinMode(vibr,INPUT); 
pinMode(green,OUTPUT);
pinMode(red,OUTPUT);
}

void loop() {
  bool value=digitalRead(vibr);
  int val;
  val=digitalRead(vibr);
  if(val==1)
  {
    digitalWrite(green,HIGH);
    delay(0);
    digitalWrite(red,HIGH);
    delay(2000); // both green and red for 24 hours represent the "danger timing". 

    bool value=digitalRead(vibr);
    if (value == 0) {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree 
      servoPin.write(pos);        
    delay(15);                      
  }
     }
   
    if (value == 1) {
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees      
    servoPin.write(pos);
    delay(15);
}
    delay(1000);

    pinMode(buzzer,OUTPUT);
    delay(0);
    tone(buzzer, 1000);
    delay(1000);        // for 1 sec
    noTone(buzzer);     // Stop sound...
    delay(1000);        // for 1sec
  }

  else
  digitalWrite(red,HIGH);
  }  digitalWrite(green,LOW);
  }
}[/CODE
[/QUOTE]
 

Attachments

ericgibbs

Joined Jan 29, 2010
15,726
hi d3,
Your Sketch had 2 Code errors, also a missing '}' character after the Else.

servoPin.write(pos); should be servo1.write(pos}

I have added the Serial IDE println so that you can monitor the progress of the Code execution.
Use the Serial IDE.
E
C-like:
// Servo  21/06/22
#include <Servo.h> //servo library

Servo servo1;
int servoPin = 5;
int pos = 0;
int vibr = 3;
int green = 12;
int red = 11;
int buzzer = 4;
int buttonState = 0;
int value = 0;

void setup() {
   Serial.begin(115200);
  Serial.println("Ready");
servo1.attach(servoPin); // sets the servo motor PIN
pinMode(vibr,INPUT);
pinMode(green,OUTPUT);
pinMode(red,OUTPUT);
}

void loop() {
  bool value=digitalRead(vibr);
  int val;
  val=digitalRead(vibr);
  if(val==1) {
   digitalWrite(green,HIGH);
   Serial.println("Green On");
    delay(0);
    digitalWrite(red,HIGH);
    Serial.println("Red On");
    delay(2000); // both green and red for 24 hours represent the "danger timing".

    bool value=digitalRead(vibr);
    if (value == 0) {
          Serial.println("Servo >180");
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      servo1.write(pos);      
    delay(15);                    
  }
     }
 
    if (value == 1) {
      Serial.println("Servo >0");
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees    
    servo1.write(pos);
    delay(15);
}
    delay(1000);

    pinMode(buzzer,OUTPUT);
    delay(0);
    Serial.println("Buzzer On");
    tone(buzzer, 1000);
    delay(1000);        // for 1 sec
    noTone(buzzer);     // Stop sound...
      Serial.println("Buzzer Off");
    delay(1000);        // for 1sec
  }else{
  digitalWrite(red,HIGH);
      Serial.println("Red On");
  }  digitalWrite(green,LOW);
      Serial.println("Green Off");
  }
}
EG 1682.gif
 

Thread Starter

d3rdlegend

Joined Jun 20, 2022
6
hi d3,
Your Sketch had 2 Code errors, also a missing '}' character after the Else.

servoPin.write(pos); should be servo1.write(pos}

I have added the Serial IDE println so that you can monitor the progress of the Code execution.
Use the Serial IDE.
E
C-like:
// Servo  21/06/22
#include <Servo.h> //servo library

Servo servo1;
int servoPin = 5;
int pos = 0;
int vibr = 3;
int green = 12;
int red = 11;
int buzzer = 4;
int buttonState = 0;
int value = 0;

void setup() {
   Serial.begin(115200);
  Serial.println("Ready");
servo1.attach(servoPin); // sets the servo motor PIN
pinMode(vibr,INPUT);
pinMode(green,OUTPUT);
pinMode(red,OUTPUT);
}

void loop() {
  bool value=digitalRead(vibr);
  int val;
  val=digitalRead(vibr);
  if(val==1) {
   digitalWrite(green,HIGH);
   Serial.println("Green On");
    delay(0);
    digitalWrite(red,HIGH);
    Serial.println("Red On");
    delay(2000); // both green and red for 24 hours represent the "danger timing".

    bool value=digitalRead(vibr);
    if (value == 0) {
          Serial.println("Servo >180");
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      servo1.write(pos);     
    delay(15);                   
  }
     }

    if (value == 1) {
      Serial.println("Servo >0");
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees   
    servo1.write(pos);
    delay(15);
}
    delay(1000);

    pinMode(buzzer,OUTPUT);
    delay(0);
    Serial.println("Buzzer On");
    tone(buzzer, 1000);
    delay(1000);        // for 1 sec
    noTone(buzzer);     // Stop sound...
      Serial.println("Buzzer Off");
    delay(1000);        // for 1sec
  }else{
  digitalWrite(red,HIGH);
      Serial.println("Red On");
  }  digitalWrite(green,LOW);
      Serial.println("Green Off");
  }
}
View attachment 269900
Oh my god the code works now and the servo is actually doing something now finally :) thanks for the help on the errors. cheers mate!
 

Attachments

Thread Starter

d3rdlegend

Joined Jun 20, 2022
6
hi d3,
Let us know how the project goes. :)
E
Hi its me again update on the project.. it seems to be doing what I was first intending it to do but I have noticed that I have seemed to lose functionality on the green led because it will only turn on with the red led on the same time when the vibration sensor is triggered any idea why? because the green led is jut an indicator that the device is turned on that just what it is so I actually tried to manipulate the code to do that by just basically making the green led on HIGH state whenever it is involved is that right? and attached file is a snapshot of the servo configuration tab should I manipulate the min and max angle because basicaly the one on proteus can do a full rotation and I just want it to do 0 to 180 and back but now it is sitting on a weird angle :/ any idea on this one?

The delays I have set is actually bugging me because it seems to not follow them tho I do understand becase im using a potato laptop lol but I have confirmed this on tinkercad which is a web based simulator I made an arduino project w/ just the servo and using a portion of the code im using it does the job perfectly but I think there is a difference on the servo used in tinkercad.

Oh and I tried the serial monitor thing but I cant port it to my arduino in proteus which is weird because it does support I did search on how to but my arduino IDE doesnt let me :/
 

Attachments

djsfantasi

Joined Apr 11, 2010
8,394
Can you post your code again? I suspect that you have an issue where the code to turn the green LED off is being executed when you don’t want it. But before you post your current code, in the IDE go to Tools at the top and click on Auto Format. It will clearly indicate the blocks of code and when they will be executed.
 

ericgibbs

Joined Jan 29, 2010
15,726
hi d3,
Downloaded and tried your Sketch, the Servo appears not to function.?
In order to construct a Sketch, I need to know exactly the sequence and timing of the project steps.

Do you know how to draw a flow chart.?
Or an explanation how you expect the project to work step by step.
E
 
Top