noob needs help on programming

Hello ,
I’m new to arduino world and programming
and i’m tryin as first project to make a useless box and it’s going well until now

i’m tryin to program different behaviors so i want to count the number of times the button is going in high position to act different on each count
so i introduced ‘byte a’ to count it and i’m having troubles making it work

here’s my code :

#include <Servo.h>

const int  buttonPin = 2;
int buttonState = 0;
int a = 0;

Servo myservo;  
int pos;               

void setup()
{
  pinMode(buttonPin, INPUT);
  myservo.attach(9); 
  byte a;
  
}

void loop()
  {
  buttonState = digitalRead(buttonPin); 
  if (buttonState == HIGH) { 
     for (a=0 ; a < 10 ; a ++);
     for (pos = myservo.read() ; pos <180 ; pos ++) {
     if (a = 1)       
       myservo.write(pos);                       
       delay(0); 
     if (a = 2)       
       myservo.write(pos);                       
       delay(10);   

     }
  }
  
  else {
     for(pos = myservo.read(); pos >=20 ; pos --) {                           
       myservo.write(pos);                                      
       delay(0); } 
       }
       }

Where in your code are you counting how many times the button pin goes HIGH ? How is the button wired ? Pull up/down resistors ? Why delay(0) ? if (a = 1)This is not testing whether a equals 1, it is setting a to 1 which will mess up the for loop and will always be true.

i was plannig to count with

 if (buttonState == HIGH) { 
     for (a=0 ; a < 10 ; a ++);

and to act different use
if a=1 { do that
if a=2 { do this

    for (a=0 ; a < 10 ; a ++);It is usual for a for loop to run some code during each iteration of the loop. The code to run is placed in braces after the definition of the for loop. What code will run for each iteration of the for loop above ?

Even if you had coded the for loop correctly that would not count how many time the button had been pressed and as I pointed out before your test for the value of a is flawed.

You need to start again. Read the state of the button and add to a variable each time that it is pressed, then act on the value of the variable. You need pull up or pull down resistors (or use the built in pull ups) to ensure that the input does not pick up random noise when the button is not pressed. You also need to consider debouncing the input to avoid picking up multiple false keypresses. You would also be wise to read up on how to recognise a change of state of a pin as opposed to when a pin is in the same state for some time.

There are examples of debouncing and state change recognition in the IDE and the reference pages will explain pinMode(pinNumber, INPUT_PULLUP);which activates the built in pull up resistors.

Declaring a variable in one function, and using it in another function, has never worked. And, never will.

zeeroo:
Hello ,
I’m new to arduino world and programming
and i’m tryin as first project to make a useless box and it’s going well until now

i’m tryin to program different behaviors so i want to count the number of times the button is going in high position to act different on each count
so i introduced ‘byte a’ to count it and i’m having troubles making it work

here’s my code :

AND HERE ARE SOME NOTES

#include <Servo.h>

const int  buttonPin = 2;
int buttonState = 0;
int a = 0;

Servo myservo;  
int pos;               

void setup()
{
  pinMode(buttonPin, INPUT);
  myservo.attach(9); 
  byte a;
  
}

void loop()
  {
  buttonState = digitalRead(buttonPin); 
  if (buttonState == HIGH) { 

ORIGINAL CODE 

     for (a=0 ; a < 10 ; a ++);

CAN YOU SEE THE DIFFERENCE ?

     for (a=0 ; a < 10 ; a ++)
    ;
FOR LOOP WHICH DOES NOTHING 



     for (pos = myservo.read() ; pos <180 ; pos ++) {
     if (a = 1)       
       myservo.write(pos);                       
       delay(0); 
     if (a = 2)       
       myservo.write(pos);                       
       delay(10);   

     }
  }
  
  else {
     for(pos = myservo.read(); pos >=20 ; pos --) {     

            
       myservo.write(pos);                                      
       delay(0); } 
       }
       }

Moderator edit: Tags seem to be causing you some problems.

Thank you all for your help

i think i need to restart i just discovered the switch/case function wich might be the right way to program i'm going to try to write a program that only count the number of time the switch is pressed first then add the servo rotation

ORIGINAL CODE

for (a=0 ; a < 10 ; a ++);

CAN YOU SEE THE DIFFERENCE ?

for (a=0 ; a < 10 ; a ++)
;
FOR LOOP WHICH DOES NOTHING

As far as the compiler is concerned there is no difference. white space (spaces, carriage returns, linefeeds, tabs) are ignored

yes i see the difference

i tought for (a=0 ; a < 10 ; a ++); was increasing the value of a by 1

i’m back to my books tryin to understand how to write the program

zeeroo:
yes i see the difference

i tought for (a=0 ; a < 10 ; a ++); was increasing the value of a by 1

i’m back to my books tryin to understand how to write the program

Yes, you were but I was pointing out to you that the for loop by itself was not doing anything useful or what you wanted…
If you put Serial.println(a) after the : you would see the a = 9 at that point.
Use Serial to help you trace your code flow, the cotcha’s like this one are easy to uncover.
The code flow is more important that the actual code.
Good luck, you are on right track.
Vaclav

Replacing your loop with a switch is not going to solve the problem.

You need to read a basic textbook or tutorial about C/C++ , so you understand the difference between a=1 and a==1 and so you learn the right places to put the semicolons.

michinyon: Replacing your loop with a switch is not going to solve the problem.

You need to read a basic textbook or tutorial about C/C++ , so you understand the difference between a=1 and a==1 and so you learn the right places to put the semicolons.

TRUE i'm actually on it i just finished the tutorial on how the use a book ;)

i just finished the tutorial on how the use a book

That made me laugh. 8)

heehee
well i have been reading a bit and came up with something … smarter (i hope) it’s not fully working but it’s closer to what it should look :

#include <Servo.h>

const int  buttonPin = 2;
int buttonState = 0;
int a = 0;

Servo myservo;  
int pos;               

void setup()
{
  pinMode(buttonPin, INPUT);
  myservo.attach(9); 
  byte a; 
}

void loop()
  {
  buttonState = digitalRead(buttonPin); 
  if ((buttonState == HIGH) && (a==0)) {
    (a+=1); 
    for (pos = myservo.read() ; pos <180 ; pos ++) {
    myservo.write(pos);                       
       delay(0);   }
     }
     buttonState = digitalRead(buttonPin);  
   if (buttonState == LOW) {
     for(pos = myservo.read(); pos >=20 ; pos --) {                           
       myservo.write(pos);                                      
       delay(0); } 
      }
   if ((buttonState == HIGH) && (a==1)) {
    (a+=1); 
    for (pos = myservo.read() ; pos <180 ; pos ++) {
      myservo.write(pos);                       
       delay(20);   }
     }
     buttonState = digitalRead(buttonPin);  
   if (buttonState == LOW) {
     for(pos = myservo.read(); pos >=20 ; pos --) {                           
       myservo.write(pos);                                      
       delay(20); } 
      }  
  }
  byte a;

This variable, local to setup(), goes out of scope immediately. Why bother?

    (a+=1);

(The) (parentheses) (are) (not) (necessary) (.)

    for (pos = myservo.read() ; pos <180 ; pos ++) {

Why does pos need to be a global variable? Hint: it doesn’t.

You haven’t written a value to the servo yet. You should remember where you told the servo to go. You should NOT expect it to tell you where you told it to go.

delay(0);   }

If you don’t want a delay, don’t call the stupid function.

NOTHING goes on the same line as the }. The { should be on a new line, too, in my opinion.

Tools + Auto Format
would do a lot
to make that messy code
more readable
.

Something you may not find in the books: It is easy to name / type variables a, b,c when you start coding. It gets harder to remember, especially when you are OF like me, their function later. Remember this when / if you learn about "Hungarian notation" - the other extreme of the spectrum.

Making templates (copy and paste ) is also helpful and there is nothing wrong including code which may not be used - as you did with delay(0). You are coding for yourself and as long it helps you that is what counts.

You should start commenting your code, just to keep track.

What you doing is fine - it is call development. Cheers Vaclav

many thanks for the comments after each reply i understand better my mistakes and how to correct them i know i'm making basic errors that must look stupid to experts anyway you got to start somewhere ...

i kept the delay (0) for testing wich part of the script is working (0 fast, 20 slow movement) and it's not supposed to be 0

btw here is how the thing is connected

Depending on the servo and the load put on it, the Arduino voltage regulator may not be able to supply enough current to drive the servo. This could, at worst, damage the regulator or could draw enough current to reduce the voltage available which might cause unexpected side effects. You would be better to use a separate external power supply for the servo with a common GND with the Arduino,

ok so i should plug the servo and a 9v battery on the other side of the breadboard and share the gnd with arduino is that right ?

The servo is more likely to need around 6 volts for which 4 AA batteries make a good source. Plug the pos and neg wires from the servo into the 6V supply, connect the 2 GNDS (negative) and connect the servo signal wire to your Arduino output pin as now.