Go Down

Topic: Servo Sketch Help (Read 171 times) previous topic - next topic

Shaen

Hi, guys! I've got a quick question about my servo Sketch.
I've got a servo and LED hooked up to my Arduino UNO along with a flame sensor. Ideally, I'd like the servo to trigger and the LED to switch on when the sensor detects a flame.
This is my sketch :

#include <Servo.h>
Servo myservo;

const int servoPin = 8;  // Servo pin on D8
const int flamePin = 6;  // Flame sensor pin on D6
const int ledPin1 = 2;  // Led on D2
int Flame = HIGH;

void setup() {
  pinMode(flamePin, INPUT);  // Flame sensor is input
  myservo.attach(servoPin);  // Servo is ouput
  pinMode(ledPin1, OUTPUT);  //LED  is output
}  //setup

void loop() {
  Flame = digitalRead(flamePin);
  if (digitalRead(flamePin) == HIGH) { 
    myservo.write(180); 
    delay(50); 
    delay(500);
    digitalWrite(ledPin1, LOW);
  }

 else if (Flame == LOW){ 
    myservo.write(0); 
    digitalWrite(ledPin1, HIGH);
  }
}

I apologise for not using the sketch box, I don't think it's possible on mobile.

The problem with my sketch is that I want the servo to stay at the open position (180°) even after the sensor stops detecting a flame. What happens instead is when the sensor detects a flame, it moves to position and then moves back when the flame is off.

Thanks in advance!

UKHeliBob

Quote
I want the servo to stay at the open position (180°) even after the sensor stops detecting a flame
So remove
Code: [Select]
myservo.write(0);
in the else clause or remove the else clause entirely
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

slipstick

So once you have detected a flame the servo should stay permanently at 180? Then just remove the whole else if clause. If you write(180) and don't ever write a different value the servo will stay at 180.

I'd also put a write(0) in setup() immediately BEFORE the attach(). That way the servo starts at 0, goes to 180 when a flame is detected and stays there.

Steve

pert

I apologise for not using the sketch box, I don't think it's possible on mobile.
I've have seen reports that the forum markup toolbar is missing in some cases. Even then, it is still possible to use code tags by adding them manually:


[code]// your code is here[/code]


Shaen

#include <Servo.h>
Servo myservo;

const int servoPin = 8;
const int flamePin = 6;
const int ledPin1 = 2;  
int Flame = HIGH;

void setup() {
 pinMode(flamePin, INPUT);  // Flame sensor is input
myservo.write(0);
 myservo.attach(servoPin);  // Servo is ouput
 pinMode(ledPin1, OUTPUT);  //Led 1 is output
}  //setup

void loop() {
 Flame = digitalRead(flamePin);
 if (digitalRead(flamePin) == HIGH) {
   myservo.write(180);
   delay(50);
   delay(500);
   digitalWrite(ledPin1, LOW);  
 }
}

Thank you for your responses! I've just tried this sketch and nothing happens when the sensor detects a flame. The sensor is functional and all wiring is correct.

AWOL

What moves the servo back?

Please remember to use code tags when posting code

Delta_G

Are you certain that what you have is a flame sensor?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

slipstick

What flame sensor are you using? Why do you read flamePin twice, once into variable Flame and then again immediately in the if? Can you add a Serial.print() to see exactly what value you are getting.
 
In this latest version what is the LED supposed to do? It never gets switched on, only off.

Steve

Go Up