Multiple loops

Just a simple question ...
Code 1:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Code 2:
#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

void setup()
{
myservo.attach(9); // 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>=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
}
}

How can you put these 2 codes together?
(With the condition that the servo turns on, when the switch is turned on.)

Thanks in advance

Before we answer, how do you think you might go about it?

Hint: you may have only 1x setup() and 1x loop()....

It sounds very easy. Put all the setup under void setup and put all the loop under void loop.
But we can't get it done. We're getting errors all the time.

Yep that's the basic approach.

So show the code so far and attach the errors... you mean compile errors?- or it compile but not work?

And as a last resort, have a look at this thread of Robin2 which is essentially your exact example.

Can't sit idle by if one of my fellow dutchies is in trouble!

kalemeister:
It sounds very easy. Put all the setup under void setup and put all the loop under void loop.
But we can't get it done. We're getting errors all the time.

Post the code where you put everything together. Probably a bracket problem...

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 9; // the number of the LED pin

int buttonState = 0; // variable for reading the pushbutton status

#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

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
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
}
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

This is what I got. This time I got no errors. But the servo doesn't do anything...

Please use code tags when posting code.

Where are your serial debug prints?

http://forum.arduino.cc/index.php?topic=148850.0

scroll down to #7

Put your code in this form, or people will complain and stop helping :wink:

How is your servo powered? Even one servo is marginal if it's powered by the arduino.

If you tell the servo to move to 180 (or zero) in setup after the attach followed by a suitable delay, does it move?

Okay sorry. We are just beginners.
Here's the code:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
const int servoPin = 9;

int buttonState = 0;         // variable for reading the pushbutton status

#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

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(servoPin, OUTPUT);  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);
    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 
    }   
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

What do you mean by debug serial print ?

kalemeister:
What do you mean by debug serial print ?

It means a serial output to check is your program is running.

#include <SoftwareSerial.h> //To include the serial code
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
const int servoPin = 9;

int buttonState = 0;         // variable for reading the pushbutton status

#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

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(servoPin, OUTPUT);  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
Serial.begin(9600); //start serial communication at 9600baud
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
Serial.println("Knop is ingedrukt"); //print to the serial monitor
    // turn LED on:    
    digitalWrite(ledPin, HIGH);
    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 
    }   
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

After uploading this, go to "Tools" and "Serial Monitor". Then select the baud rate of 9600 (Bottom right) and see if there is an output if you press the button.

When we open the monitor, it says: "Knop is ingedrukt".
When we switch the lever, the monitor doesn't react to it.

kalemeister:
When we open the monitor, it says: "Knop is ingedrukt".
When we switch the lever, the monitor doesn't react to it.

Let me guess, the monitor keeps giving the output and stops when you press the button/lever?
How did you connect the button/leaver electrically? Is the LED on all the time until you press the button/lever?

Put some more serial prints in, to help you see where the logic takes you.

if (buttonState == HIGH) {    
Serial.println("Knop is ingedrukt"); //print to the serial monitor
    // turn LED on:    
    digitalWrite(ledPin, HIGH);
    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' 
Serial.println("servo increment"); //print to the serial monitor <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      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' 
Serial.println("servo decrement"); //print to the serial monitor <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      delay(15);                       // waits 15ms for the servo to reach the position 
    }   
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
Serial.println("button not pushed"); //print to the serial monitor <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  }
}
#include <SoftwareSerial.h> //To include the serial code

Huh?
Why bother with SoftwareSerial?

AWOL:

#include <SoftwareSerial.h> //To include the serial code

Huh?
Why bother with SoftwareSerial?

Not paying attention... brainfart!

AWOL's right.

C-F-K:

kalemeister:
When we open the monitor, it says: "Knop is ingedrukt".
When we switch the lever, the monitor doesn't react to it.

Let me guess, the monitor keeps giving the output and stops when you press the button/lever?
How did you connect the button/leaver electrically? Is the LED on all the time until you press the button/lever?

Yeah, it says: 'Knop is ingedrukt' (just 1 time). And nothing else. Also not when we switch the lever.

JimboZA:
Put some more serial prints in, to help you see where the logic takes you.

Servo increment (multiple times)
Then:
Servo decrement (multiple times)
Then it stops

kalemeister:

C-F-K:

kalemeister:
When we open the monitor, it says: "Knop is ingedrukt".
When we switch the lever, the monitor doesn't react to it.

Let me guess, the monitor keeps giving the output and stops when you press the button/lever?
How did you connect the button/leaver electrically? Is the LED on all the time until you press the button/lever?

Yeah, it says: 'Knop is ingedrukt' (just 1 time). And nothing else. Also not when we switch the lever.

Again: How did you connect the button electrical? Can you draw the schematic and upload it or something...?

Ok try this.

I reversed the logic: changed the button from INPUT to INPUT_PULLUP, and look for a LOW on the button not a HIGH. (edit.... I did that since I suspect you have no pulldown resistor, but instead of me wiring one in, it's easier to go internal pullup. That reverses the logic though...)

 if (buttonState == LOW) {  

****************CHANGE THE BUTTON WIRING TO BE FROM PIN TO GROUND NOT 5V

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
const int servoPin = 9;

int buttonState = 0;         // variable for reading the pushbutton status

#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

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(servoPin, OUTPUT);  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
Serial.begin(9600); //start serial communication at 9600baud
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {    
Serial.println("Knop is ingedrukt"); //print to the serial monitor
    // turn LED on:    
    digitalWrite(ledPin, HIGH);
    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' 
      Serial.println("servo +"); //print to the serial monitor
      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 
     Serial.println("servo -"); //print to the serial monitor
    {                                
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    }   
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
     Serial.println("not pushed"); //print to the serial monitor
  }
}

You'll realise that once you press the button and the test for pressed is passed, the servo completes its journey even when you release the button.