Push Button, Run Servo, HELP!

Hello People,

I really need some help!
I can't get mij servo running after I push my FSR (push-sensor).

The loop should be as followed:
Push the FSR
If the FSR is Pushed, start the Servo
After the Servo has made his 180 degree turn and has gone back to the 0 degree position, three LEDs should get burning after each other.

I've got the servo running and afterwards the LEDs go on, but this action must be activated by pushing the FSR!

I've got some code, but I can't combine them, cause my knowledge isn't that good! Can someone please help me?!?!

The code for activating the Servo and afterwards the LEDs:

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

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

int ledPin = 9 ; // LED connected to digital pin 9
int ledPin1 = 10 ;
int ledPin2 = 11 ;
int ledPin3 = 3;

// The setup() method runs once, when the sketch starts

void setup() {

myservo.attach(5); // attaches the servo on pin 5 to the servo object

// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

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 >=1; 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
}

digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin1, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin2, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin3, HIGH); // set the LED on
delay(1000); // wait for a second
}

And the code for the FSR:
const int analogPin = 5; // pin that the sensor is attached to
const int ledPin = 11; // pin that the LED is attached to
const int threshold = 20; // an arbitrary threshold level that's in the range of the analog input

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}

void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);

// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin,LOW);
}

// print the analog value:
Serial.println(analogValue, DEC);

}

Someone PLZ PLZ PLZ help?! All my thanks in advance!

In your second code, you have this:

// if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin,LOW);
  }

Replace the first digitalWrite statement with the code to move the servo and light the LEDs.

Remove the whole else block.

Thank you for your response! We allready tried that, and we think you mean that the code should be as followed:

#include <Servo.h>

Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position

const int analogPin = 5; // pin that the sensor is attached to
const int threshold = 20; // an arbitrary threshold level that's in the range of the analog input

int ledPin = 9 ; // LED connected to digital pin 9
int ledPin1 = 10 ;
int ledPin2 = 11 ;
int ledPin3 = 3;

void setup() {

myservo.attach(5); // attaches the servo on pin 5 to the servo object

// initialize serial communications:
Serial.begin(9600);

pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}

void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);

if (analogValue > threshold){

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 >=1; 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
}

digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin1, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin2, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin3, HIGH); // set the LED on
delay(1000); // wait for a second

}
}

The weird thing is:
The systems runs one time automaticly (without pushing the FSR).
After this loop is completed, than we can run the system by pushing the FSR?!

How is that possible?!

Thank you again! You've been a great help so far!

I'm not sure why the servo and lights work once without touching the FSR. I'd try declaring that analogPin is an INPUT pin.

I'd also add a Serial.print statement after reading the sensor value, to see what value was read. Add a delay after the if block, so you don't stream out too much data.

I'd try declaring that analogPin is an INPUT pin

Unhelpful, and potentially troublesome.

Quote:
I'd try declaring that analogPin is an INPUT pin

Unhelpful, and potentially troublesome.

Why and how?

const int analogPin = 5;     // pin that the sensor is attached to
//
//
void setup() {
  pinMode (analogPin, INPUT);

Like that?
Why would you want to do that?

No? OK imagine:

const int analogPin = 5;     // pin that the sensor is attached to
...
...
...
const int LED_PIN = 5; 
...
void setup() {
  pinMode (LED_PIN, OUTPUT);
...
...
  pinMode (analogPin, INPUT);

Why wouldn't I? I want to read data from that pin. Therefore, it is an INPUT pin.

What is the problem with doing it? What potential trouble can arise as a result of doing it?

Therefore, it is an INPUT pin.

Correction: it is an analogue input pin.

Thank you all, but we managed to get it right!
It had to do with the fact that in an 'if' statement you'll need an 'else' statement also.

The code has become like this:

#include <Servo.h>

Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position

const int analogPin = 5; // pin that the sensor is attached to
const int threshold = 20; // an arbitrary threshold level that's in the range of the analog input

int ledPin = 9 ; // LED connected to digital pin 9
int ledPin1 = 10 ; // LED connected to digital pin 10
int ledPin2 = 11 ; // LED connected to digital pin 11
int ledPin3 = 3; // LED connected to digital pin 3

void setup() {

myservo.attach(5); // attaches the servo on pin 5 to the servo object

pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}

void loop() {

int analogValue = analogRead(analogPin); // read the value of the potentiometer

if (analogValue > threshold){

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 >=1; 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
}

digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin1, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin2, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin3, HIGH); // set the LED on
delay(1000); // wait for a second
delay(999999999); // wait for a lot of seconds
}

else{
myservo.write(0);
}

}

Do you really only want to move the server once, when the FSR is pressed?

delay(999999999);            // wait for a lot of seconds

Yes I do! :slight_smile: