-2

In this project I want smooth rotation of the Servo(Pin (D10) periodically where period time select by a potentiometer(actually I will use pressure sensor in this pin now it is unavailable to me so I am using g potentiometer).The Time period controlled by by a potentiometer position(two) change at Pin A0.Two Led(blue & red) (D7,D9)(in this pin I will use two valve controller servo) blink with same period and other yellow Led blink same period but oppositely.All working except Servo which rotation is not smooth .It's rotation sometime maintain proper period and sometime unnecessarily stop.Before I tried TimerOne and Servo Library but they are creating some conflict and not uploaded to Board.I am using Arduino Uno Board.What change should I do either in Circuit or in Code.I am new in this field.Any kind help would be grateful for me thank you.And my code is

#include <TimerOne.h>
#include "ServoTimer2.h"
const int potPin = A0;
const int bluePin =7;
const int redPin =8;
const int whitePin = 9;
const int yellowPin = 11;
const int myServoPin = 10;
ServoTimer2 myServo;
int angle;
int speed;
int potVal;
int breathPress;
int lowPress=12;
int upPress=24;
int lowCount=0;
int upCount =0 ;
int time1;
int time2;
int timeDiff;
int temp;
int potPin1 = A2;
int breathRatio;
int ratioVal;
void setup() {
  pinMode(potPin,INPUT);
  pinMode(bluePin,OUTPUT);
  pinMode(redPin,OUTPUT);
  pinMode(potPin1,INPUT);
  pinMode(whitePin,OUTPUT);
  pinMode(yellowPin,OUTPUT);
  myServo.attach(10);


  Timer1.initialize(100000);
  Timer1.attachInterrupt(Blink);
  Serial.begin(9600);
 }

void loop() {
  breathRatio=analogRead(potPin1);
  ratioVal = map(breathRatio,0,1023,1,6);

  digitalWrite(bluePin,HIGH);
  digitalWrite(yellowPin,HIGH);
  digitalWrite(whitePin,LOW);
  myServo.write(90);


  delay(timeDiff);
  digitalWrite(bluePin,LOW);
  digitalWrite(yellowPin,LOW);
  digitalWrite(whitePin,HIGH);
  myServo.write(60);
  delay(timeDiff);

}

void Blink(){
  potVal = analogRead(potPin);
  breathPress = map(potVal,0,1023,0,30);
  Serial.print("Pressure=");
  Serial.println(breathPress);
  Serial.println(time1);
  Serial.println(time2);
  Serial.print("Time Difference :");
  Serial.println(timeDiff);
  if(breathPress<10){
  digitalWrite(redPin,LOW);
  }
  if(breathPress>27){
    digitalWrite(redPin,LOW);
  }
  else {
    digitalWrite(redPin,HIGH);
  }
  if ((breathPress - 10 )<= 0){
    lowCount = 1;
    upCount =0;
    time1 = millis();
  }
  if((breathPress - 27)>=0){
    lowCount = 0;
    upCount = 1;
    time2 = millis();
  }
  if(time1-time2 >= 0){
    timeDiff = time1-time2;
  }
  else{
    timeDiff = time2-time1;
  }

And output showing

13096 14994 Time Difference :1898 Pressure=15 13096 14994 Time Difference :1898 Pressure=15 13096 14994 Time Difference :1898 Pressure=15 13096 14994 Time Difference :1898 Pressure=15

  • 1
    your post looks like a book exploded ... you will wait a long time before someone spends their time reading that wall of words ... sentences have a space after a period ... related thoughts are grouped into paragraphs – jsotola Apr 15 at 3:58
  • If you used a "Blink Without Delay" style if statement and millis to handle the timing for both this and the leds then you could use the normal Servo library and your life would be much much much easier. – Delta_G Apr 15 at 5:20
  • You copied your question text 2 times into the question. I removed the duplicated text for you – chrisl Apr 15 at 14:39
0

What you are claiming does not makes sense to me. In your code you are not moving the sensor smoothly. You are setting it to 60 and 90 degrees successively. You don't control the movement between these values. That's, what the servo does. It moves directly, where you tell it to move. You cannot control how the servo moves between two values, that you set. To move slower (you could call that smooth) between the values, you would need to set the inbetween values for the servo. So instead of going directly from 60 to 90, you would go in steps, from 60 to 61, to 62, ... to 89, to 90 degrees.

Also you are not controlling the servo with the Timer1 library, at least not directly. You measure a time difference with the library. This time difference is then the time, that the servo will stay in the current position, until it is moved again.


Note, that this

if((breathPress - 27)>=0)

is an over-complicated way of writing

if(breathPress >= 27)
| improve this answer | |
  • by using for loop?actually I tried that but there is no motion of Servo. – baponkar Apr 15 at 14:53
  • Yes, for example with a for loop. We don't see your former code. Also your current question is rather ill stated. I understood about half of it. Please improve your question, so that it is more clear, what your problem is. If you already knew, that you need to use a for loop, why not asking about why your for loop code does not work? – chrisl Apr 15 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.