different action based on the number of times a button is pressed

Hi,

I've modified my code without the goto's and the program is working ok

#include <Servo.h> 

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

float pos = 0;    // variable to store the rotary switch input



void setup() {
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo1.write(pos);
  delay(3000);
}

// the loop routine runs over and over again forever:
void loop() {

  int sensorValue = analogRead(A0);
  if (sensorValue > 100 && sensorValue < 200) {
    pos=10;
  }
  else if (sensorValue > 200 && sensorValue < 400) {
    pos=13;
  }
  else if (sensorValue > 400 && sensorValue < 600) {
    pos=14;
  }  
  else if (sensorValue > 600 && sensorValue < 700) {
    pos=15;
  }
  else if (sensorValue > 700 && sensorValue < 900) {
    pos=16;
  } 
  else if (sensorValue > 900) {
    pos=17;
  } 


  myservo1.write(pos);

}

But then I added a basic rise and fall code for the first rotary switch position but get the error:
_13_prog.cpp: In function 'void loop()':
_13_prog:44: error: 'else' without a previous 'if'

I removed the else and the code compiled, but when I tested the air raid it only ran the rise and fall code and not the other switch positions

#include <Servo.h> 

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

float pos = 0;    // variable to store the rotary switch input



void setup() {
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo1.write(pos);
  delay(3000);
}

// the loop routine runs over and over again forever:
void loop() {

  int sensorValue = analogRead(A0);
  if (sensorValue > 100 && sensorValue < 200) 
   {
    pos=12.5;
  }
  // rise and fall code                      
  for(pos = 12.5; pos < 15; pos += .1)  // goes from 12.5  to 15 
  {                                  // in steps of .1  
    myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(25);                       // waits 15ms for the servo to reach the position 
  } 

  delay(5000);

  for(pos = 15; pos>=12.5; pos-=.1)     // goes from 15  to 12.5 
  {                                
    myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(25);                       // waits 15ms for the servo to reach the position 
  } 


   else if (sensorValue > 200 && sensorValue < 400) // error on this line 
  {
    pos=13;
  }
  else if (sensorValue > 400 && sensorValue < 600) {
    pos=14;
  }  
  else if (sensorValue > 600 && sensorValue < 700) {
    pos=15;
  }
  else if (sensorValue > 700 && sensorValue < 900) {
    pos=16;
  } 
  else if (sensorValue > 900) {
    pos=17;
  } 


  myservo1.write(pos);

}

Where am I going wrong with this piece of code? Any infomation would be gratefully recieved

Regards

Dale

Dale, the simple format of if is

if (condition)
{
code if true
}

In your case you need to put everything you want to happen "if true" within those braces:

...
  if (sensorValue > 100 && sensorValue < 200) 
   {
      set position
      rise code
      delay
      fall code                      
   }
   else if (sensorValue > 200 && sensorValue < 400) 
... etc

Keeping things indented properly helps you keep track of the braces. And the auto-format tool also. (on the Tools Menu)

Cheers,
John

Thanks John,

Code modified and running ok, much appreciated.

Hazzard thanks for the upload, I was thinking more of a count then time based, but thinking more about it, if i did go down the count method I would need to have a way to re-set the counter and time based is maybe the way to go. Hmm my hurts. I need to wire these switches to my breadboard and start experimenting.

Regards

Dale

Hi,

I thought I had it sorted when I testing last night, the wife was giving me the evil eye on testing the siren so late. turns out that it is not rising and falling. I think it's due to it doing everthing between the braces which is increment up .1 then delay and then increment down .1 which keeps the speed the same, which is whats happining. I posted the code below with better coments

void loop() {

  int sensorValue = analogRead(A0);
  if (sensorValue > 100 && sensorValue < 200) 
  {
   

    // rise and fall code                      
    for (pos = 12.5; pos < 18; pos += .1)  // goes from 12.5  to 12.6
            myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(25);                       // waits 15ms for the servo to reach the position 
// because there is no braces code does not loop and reach pos 18, but moves onto next line

    delay(1000);

    for (pos = 18; pos>=12.5; pos-=.1)     // this one is odd as I should hear it jump from 12.6 to 18 
                                                           //  but it seems a constant speed, no change in sound anyway

      myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(25);                       // waits 15ms for the servo to reach the position 
  }                                       // loop back

I went back to I to an old program just to check it worked with the braces and it did code below.

void loop() 
{ 

  for(pos = 12.5; pos < 18; pos += .1)  // goes from 12.5 degrees to 18 degrees in .1 increments
  {                                  
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15    );      // waits 15ms for the servo to reach the position
  }
// loop until pos = 18
  {

    delay (5000);  

  } 
  for(pos = 18; pos>=12.5; pos-=.1)     // goes from 18  to 12.5  in .1 increments
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
// loop until pos = 12.5
}

Am I on the right lines in my thinking, seems it's a formatting error but I don't know the correct format. Any information gratefully recieved

Regards

Dale

What type is pos? If it is an int, than you can't assign 12.5 to it. Writing 12.5 to a servo doesn't make sense, either, since the Servo::write() method expects an int.

// because there is no braces code does not loop and reach pos 18, but moves onto next line

Well, you ought to have them, even if they are not strictly needed. Having them makes them look like you know what you are doing.

If you didn't add this comment, why have you ignored it?

The other code has some braces that you need and some that you do not. You need to learn to tell the difference.

Yes Dale.

"More cowbells! I need more cowbells!"

cowbells == braces

http://www.cplusplus.com/doc/tutorial/control/

And put the delay(25) inside them, so it will be part of your for loop

John

Hi paul

I'll post the entire code below

#include <Servo.h> 

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

float pos = 0;    // variable to store the rotary switch input



void setup() {
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo1.write(pos);
  delay(3000);
}

// the loop routine runs over and over again forever:
void loop() {

  int sensorValue = analogRead(A0);
  if (sensorValue > 100 && sensorValue < 200) 
  {
   // pos=12.5;

    // rise and fall code                      
    for (pos = 12.5; pos < 18; pos += .1)  // goes from 12.5  to 18 
                                                          // in steps of .1  
      myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(25);                       // waits 15ms for the servo to reach the position 


    delay(1000);

    for (pos = 18; pos>=12.5; pos-=.1)     // goes from 18  to 12.5  

      myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(25);                       // waits 15ms for the servo to reach the position 
  } 

  else if (sensorValue > 200 && sensorValue < 400) // error on this line 
  {
    pos=13;
  }
  else if (sensorValue > 400 && sensorValue < 600) {
    pos=14;
  }  
  else if (sensorValue > 600 && sensorValue < 700) {
    pos=15;
  }
  else if (sensorValue > 700 && sensorValue < 900) {
    pos=16;
  } 
  else if (sensorValue > 900) {
    pos=17;
  } 


  myservo1.write(pos);

}


Pos is a float as I want more control over the rise and fall of my motor

Well, you ought to have them, even if they are not strictly needed. Having them makes them look like you know what you are doing.

If you didn't add this comment, why have you ignored it?

The other code has some braces that you need and some that you do not. You need to learn to tell the difference.

I don't think I explained it very well, originally I had it with braces but I got an error with my else before if statement, to get the program to compile as john pointed out i need to place all the rise a fall code in between the braces. but this causes the program to loop the rise and fall part between the braces.

Regards

Dale

P.S. John just posted as i was writing this, cheers I'll check those links out right now

If statements need braces
for loops need braces
while loops need braces
... etc

Almost everything almost always needs braces

float pos = 0;    // variable to store the rotary switch input
    for (pos = 12.5; pos < 18; pos += .1)  // goes from 12.5  to 18 
                                                          // in steps of .1  
      myservo1.write(pos);              // tell servo to go to position in variable 'pos'

I fail to see where any rotary switch is read. So, the initial comment is crap.

The only place that pos is used is to position the servo. The Servo::write() function takes an int.

So, why is pos a float?

The Tools + Auto Format function would do an excellent job of properly indenting your code, so you can see where braces are missing. Indenting a statement doesn't mean squat to the compiler. But, it means a lot to people reading the code. Properly indented code has the same meaning to people and to compilers. The auto format tool makes the indenting match how the compiler will understand the code. It can, then, be quite obvious that how you understand it and how the compiler will understand it are not the same thing.

I can not stress the importance of properly indented code enough. Yours, of course, is not.

johncc's comment regarding braces is nonsense. If statements, and for and while loops, may or may not need braces. It is recommended that they always be used, so that you can add a statement to the block, and be sure that the statement is executed the correct number of times. Switch statements always need braces.

Functions always need curly braces. Almost any other use of curly braces (except in array initializations) is unnecessary.

John thanks for the second link, I've done a copy and paste

Here is an example of countdown using a for loop:

1234567891011
// countdown using a for loop
#include
using namespace std;
int main ()
{
for (int n=10; n>0; n--) {
cout << n << ", ";
}
cout << "FIRE!\n";
return 0;
}
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!

I don't think there is a better way to learn then to see an example of how its done.
Here is my working code: with better comments

#include <Servo.h> 

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

float pos = 0;    // variable to store the rotary switch input



void setup() {
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo1.write(pos);
  delay(3000);
}

// the loop routine runs over and over again forever:
void loop() {

  int sensorValue = analogRead(A0); // read value from rotary switch
  if (sensorValue > 100 && sensorValue < 200) // rotary switch position 1
  {


    // rise and fall code                      
    for (pos = 12.5; pos < 18; pos += .1){  // goes from 12.5  to 15 
      // in steps of .1  
      myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(25);                       // waits 15ms for the servo to reach the position 
    }

    delay(3000);

    for (pos = 18; pos>=12.5; pos-=.1) {    // goes from 15  to 12.5  

      myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(25);     // waits 15ms for the servo to reach the position 
    } 
  }
  else if (sensorValue > 200 && sensorValue < 400) // rotary switch position 2
  {
    pos=13;
  }
  else if (sensorValue > 400 && sensorValue < 600) {// rotary switch position 3
    pos=14;
  }  
  else if (sensorValue > 600 && sensorValue < 700) {// rotary switch position 4
    pos=15;
  }
  else if (sensorValue > 700 && sensorValue < 900) {// rotary switch position 5
    pos=16;
  } 
  else if (sensorValue > 900) {// rotary switch position 6
    pos=17;
  } 


  myservo1.write(pos);

}

I really happy with how the program is coming along, due to all the help I have recieved. Next thing on my to do list is wire the switches to my breadboard and start experimenting.

John I was looking Jeremy Blum vid on youtube and seen a familiar name in the recommendations John NYCCNC is that you?
I've subscribed anyway, lots of very interesting vids to look at. I am a cnc'er myself, I've got a small vmc which I also use to turn small parts on which is what I made the air raid on, I've also biult my own router table.

Regards

Dale

Oops

I have'nt been doing a good job on my comments,I was going to tidy them when I got a finally rev of the program but can see this one is misleading.

float pos = 0; // variable to store the rotary switch input

now reads

float pos = 0; // variable for servo positon

regards

Dale

now reads

Fixing the comment is far less important than fixing the underlying problem which is that pos should be an int, not a float.

PaulS:
Functions always need curly braces. Almost any other use of curly braces (except in array initializations) is unnecessary.

Right. But my point was simply that I think the curly braces below are necessary, and arduinodale didn't have them

    for (pos = 12.5; pos < 18; pos += .1){  // goes from 12.5  to 15 
      // in steps of .1  
      myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(25);                       // waits 15ms for the servo to reach the position 
    }

arduinodale:
John thanks for the second link, I've done a copy and paste

Here is an example of countdown using a for loop:
1234567891011

// countdown using a for loop

#include
using namespace std;
int main ()
{
 for (int n=10; n>0; n--) {
   cout << n << ", ";
 }
 cout << "FIRE!\n";
 return 0;
}



10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!

I don't think there is a better way to learn then to see an example of how its done.

Actually that is an example showing a case where the braces are not required, sorry . You only really need a "compound statement" aka "block" (with the braces) , as a way of putting more than one statement as part of your loop. But in this case they don't hurt, and are even recommended by many (because it doesn't hurt for a block to have only one statement). It is explained more thoroughly at the top of that page.

Here is my working code: with better comments

Awesome, glad its working better now!

John I was looking Jeremy Blum vid on youtube and seen a familiar name in the recommendations John NYCCNC is that you?

No, 'fraid not :slight_smile:

Cheers,
John

Thanks for all the people that have helped (mainly John) on this project and those that have been less than, why bother posting a reply. I did try to get the button code working but it's way beyond me, After researching around I found the best example was in the examples/digital/state change detection. I did try and hack this code into my prog but have admitted defeat.

Kind regards

Dale

With the hope you check back here several more times ...

I don't know much about you but myself I do a lot of my problem solving in the background when I'm not giving any conscience effort to it - usually while sleeping.

My suggestion is not to admit defeat but simply set it aside for awhile relax some and come back to it at a later date.

Thanks for all the people that have helped (mainly John) on this project and those that have been less than, why bother posting a reply.

It's hard to know what will help a person. Sometimes a hint or a gentle nudge or a bit of encouragement is all that a poster needs. Sometimes the poster needs to have there hand held for a while.

If you try holding someone's hand when all they need is a hint, they don't like it. If all you give is a hint to someone that needs there hand held for a while, they don't like it.

You need to remember that the pay here isn't all that great. We're all volunteers. Sometimes that works; sometimes it doesn't.

If the project is important to you, and is beyond your skill set, you have two choices. Expand your skill set (learn!) or head over to Gigs and Collaboration, and offer to pay for the help you need.

Whining about people that try to help is not the correct answer.

This forum is for help and is unique from all others I have used, in that the amount of comments posted that are unhelpful. If somebody asks for a hint then give them a hint, it's that simple. but most on here are noobs asking for help.

You need to remember that the pay here isn't all that great. We're all volunteers. Sometimes that works; sometimes it doesn't.

If the project is important to you, and is beyond your skill set, you have two choices. Expand your skill set (learn!) or head over to Gigs and Collaboration, and offer to pay for the help you need.

I've got a few "how to" videos on youtube machining related, but I'm not asking a payment for them, just done it to help others. This is what makes the internet great, expanding people's minds.

Regards

Dale

If somebody asks for a hint then give them a hint, it's that simple. but most on here are noobs asking for help.

The problem is that someone that just needs a hint or a nudge asks for help. Someone that needs their hand held for a while asks for help. How are we to decode "I need help"?

How would you feel if, after watching one of your videos, a viewer posted a comment that said "Well, that wasn't helpful at all"? I'm sure that you would think "Well, so what? You got what you paid for! Other people appreciated it.".

Paul this could go on for a long time, which you have a lot of.

Logging out

Dale