Error Message using DO WHILE Statement

Hi I am having problems with my code. It gives me the errors sketch_oct16c:12: error: expected unqualified-id before 'switch' sketch_oct16c:21: error: expected initializer before 'switchState' sketch_oct16c:22: error: expected unqualified-id before '{' token

This is the sketch:

#include  

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 potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 

int switch = 9;
int switchState = 0;

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

void loop() 
switchState = digitalRead(switch);
{do
  { 
     // scale it to use it with the servo (value between 0 and 180) 

  for(pos = 0; pos < 90; pos += val)  // goes from 0 degrees to 180 degrees 
  {    
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 1, 30);    // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(400);                       // waits 15ms for the servo to reach the position 
  } 

    for(pos = 60; pos < 180; pos += val)  // goes from 0 degrees to 180 degrees 
  {  
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 1, 30);    // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(700);                       // waits 15ms for the servo to reach the position 
  } 

  for(pos = 180; pos>=1; pos-= val)     // goes from 180 degrees to 0 degrees 
  {    
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 1, 30);    
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(400);                       // waits 15ms for the servo to reach the position 
  } 

  delay(2000);
} while ( switch = 1);
/code]

In the sketch the pot acts as a speed controller for the servo sweep. I want to attach a switch (toggle switch) that puts on or off the continous sweep. Thanks

You haven't initialised the digital pins and the brace is in the wrong place in "void loop()"

switch is a key word, you can't use it for a variable name.

while ( switch = 1);

That is also going to give you some grief.

= assignment operator == comparison operator

The semicolon on the end of that statement makes the while loop an infinite loop, since the condition variable doesn't change in the empty body.

Thanks but i'm fairly new at this. Does anyone know what the final code will look like?

Does anyone know what the final code will look like?

You all get started coding. I'll go find out what the requirements are.

That's what you're asking us to do. I'll wait.

The requirements are simple. The first part of the code works perfectly. It uses the pot as a speed controller while in a sweep function. I intend to add a toggle switch which basically turns the continous sweep on or off and this is where i have a problem.

The first part of the code works perfectly.

How can it, when you can't even compile it?

This is the first part of the code. Like i explained, it works. I decided to add a start/stop switch as an added incentive. If anyone has an idea of how that can be done, that'll be helpful. Thanks / Sweep with speed control // by jedkoch http://jedkoch.com

include

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 potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin

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

void loop() { // scale it to use it with the servo (value between 0 and 180)

for(pos = 0; pos < 90; pos += val) // goes from 0 degrees to 180 degrees { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 1, 30); // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(400); // waits 15ms for the servo to reach the position }

for(pos = 60; pos < 180; pos += val) // goes from 0 degrees to 180 degrees { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 1, 30); // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(700); // waits 15ms for the servo to reach the position }

for(pos = 180; pos>=1; pos-= val) // goes from 180 degrees to 0 degrees { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 1, 30); myservo.write(pos); // tell servo to go to position in variable 'pos' delay(400); // waits 15ms for the servo to reach the position }

delay(2000); }

Any code you want to add to the loop() function goes INSIDE the body of the loop function. Do you know what the start of the body of a function looks like? Here's a hint: {.

Where is the codeYOU added? BEFORE the {. It needs to be AFTER the {.

There are reserved words, like switch. You should use variable names that indicate the function of the value that the variable holds. Your variable switchState holds the state of the switch. That's a good name. Your variable switch holds a pin number. That's a lousy name. The name switchPin would be better, AND it would be legal.

Do/while statements are the worst possible statements, in my opinion. The check for whether the action should be performed is not tested until the end. So, you start the Arduino, and the servo waves around once, without you pressing the switch. Then, it stops until you press the switch. That is likely NOT what you want. So, don't use a do/while. Use a while statement, instead.

Thanks tried your suggestion with a while statement and changed it up a little bit but still getting little errors. This is the code:

#include  

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 potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 

int switchPin = 9;
int switchState = 0;

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode (switchPin, INPUT);
} 

void loop ( ) 
{
  //while button is high, run sweep
  while (digitalRead (switchState) == 1) {
    loop2();
  

void loop2() 
{ 
     // scale it to use it with the servo (value between 0 and 180) 

  for(pos = 0; pos < 90; pos += val)  // goes from 0 degrees to 180 degrees 
  {    
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 1, 30);    // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(400);                       // waits 15ms for the servo to reach the position 
  } 

    for(pos = 60; pos < 180; pos += val)  // goes from 0 degrees to 180 degrees 
  {  
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 1, 30);    // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(700);                       // waits 15ms for the servo to reach the position 
  } 

  for(pos = 180; pos>=1; pos-= val)     // goes from 180 degrees to 0 degrees 
  {    
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 1, 30);    
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(400);                       // waits 15ms for the servo to reach the position 
  } 

}delay(2000);
} /code]

You are now trying to define a function, loop2(), inside another function, loop(). That is not allowed.

Thanks I saw my error and moved the loop and i don't get that error any more . I still get a couple of errors though on lines 51  and 52. it says:
sketch_oct17a:51: error: expected constructor, destructor, or type conversion before '(' token
sketch_oct17a:52: error: expected declaration before '}' token

i THINK I DIDNT PLACE the brackets in the correct position


[code
#include  

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 potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 

int switchPin = 9;
int switchState = 0;

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode (switchPin, INPUT);
} 


  void loop() 
{ 
     // scale it to use it with the servo (value between 0 and 180) 

  for(pos = 0; pos < 90; pos += val)  // goes from 0 degrees to 180 degrees 
  {    
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 1, 30);    // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(400);                       // waits 15ms for the servo to reach the position 
  } 

    for(pos = 60; pos < 180; pos += val)  // goes from 0 degrees to 180 degrees 
  {  
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 1, 30);    // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(700);                       // waits 15ms for the servo to reach the position 
  } 

  for(pos = 180; pos>=1; pos-= val)     // goes from 180 degrees to 0 degrees 
  {    
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 1, 30);    
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(400);                       // waits 15ms for the servo to reach the position 
  } 

}delay(2000);
} 

void loop2 ( ) 
{
  //while button is high, run sweep
  while (digitalRead (switchState) == 1) {
    loop();

Thanks I saw my error and moved the loop and i don’t get that error any more . I still get a couple of errors though on lines 51 and 52. it says:
sketch_oct17a:51: error: expected constructor, destructor, or type conversion before ‘(’ token
sketch_oct17a:52: error: expected declaration before ‘}’ token

i THINK I DIDNT PLACE the brackets in the correct position

the code i changed is just above

Put every { and } on separate lines. Use the tools/autoindent command to indent your code correctly. It will then be obvious that you have code outside of your functions.

Delete everything from 'void loop2 ( )' onwards since it is nonsense.

If i delete loop 2 how do i use the while statement to tell the arduino to continue running the sweep loop until the switch is turned off?

you cannot define a function inside the loop() function, but you can call it :

void loop ( ) 
{
  //while button is high, run sweep
  while (digitalRead (switchState) == 1) {
    loop2();
  

void loop2() 
{ 
     // scale it to use it with the servo (value between 0 and 180)

should be :

void loop()
{
     if(digitalRead(switchState) == 1)    // remember loop()  runs repeatedly, this test is done each time, so, no need of 'while'
     {
           loop2();
           // other instructions if needed here
     }
     else     //optional, you can add instructions which will be executed if switchState is not '1'
     {
            // code executed if the previous digitalRead did not return a '1'  
     }
  // other instructions here 
}

// here you define your loop2() function
void loop2()
{
    //instructions here
}

if you need to run loop2() as long as the switch is "1" , you can put the while statement in loop2()

void loop2()
{
    while(digitalRead(switchState==1))
    {
           //code here
    }
}

in this case, loop2() will be executed as soon as switchState is "1" and will run until it is not "1" but be aware that the other instructions in loop() won't be executed while switchState is "1"

Thanks it makes much more sense now.

One question though:

Can both loops be run together (lets say linked)

So the continous sweep with the pot as a speed control is loop one and the on off switch is loop two

So while loop one runs automatically, loop two starts or stops loop one depending on its state (high or low)

Can both loops be run together (lets say linked)

Would that make sense?

So while loop one runs automatically, loop two starts or stops loop one depending on its state (high or low)

Loop one waves the sensor around in a continuous motion.

Loop two waves the sensor around in a continuous motion when the switch is pressed.

Exactly what does the switch do?

It looks to me like you want to do nothing when the switch is not pressed, and do something when the switch is pressed.

I think, aside from not being familiar with the syntax and structure of Arduino programs, that your biggest problem is that you can't even define the requirements for the program you want. Until you learn to define the requirements in an unambiguous way, you will continue to struggle.