Help, Proggraming Button

My button is connected to digital pin, when I dont touch it (it's not pressed) it gives me the value "1" when I Press it (It's pressed) it gives me the value "0" I want that for every time I push the button it will print something else. The thing is, that I dont wanna hold the bottun, I want that every press will change the "printing mode", without the need of holding it. So how can I do that? For example, Not touched print corrent mode ('c' for example) First push will print 'x' Next push will print 'y' Next push will print 'x' .... And so on.

The thing is, that I dont wanna hold the bottun, I want that every press will change the "printing mode", without the need of holding it.

Look at the StateChangeDetection example in the IDE

It illustrates how to detect when a switch [u]becomes[/u] pressed rather than when it [u]is[/u] pressed. Count how many state changes there have been and use the number to get data from an array of responses

I will give it a try, Thank you mate. Btw, What are those "karma:xxxx[add]" mean.

What are those "karma:xxxx[add]" mean.

If you click on it then one Karma point will be added to the poster. It is a way of saying thanks but has no other meaning.

I got a problem with this example, Beacuse my button, allways go back to "1" and the programm will count it as change, When Push it changes from 1 to 0 and the push is only about a second so it comes back to 1. I need that it will only count the 1 to 0 changes, but it gives me a new problem, beacuse when I write if(Correntvalue=0) I cant have a second mode, the print of 'y' as i described previously. I'm confused. Help.

The panic all gone, I think this will work.

int LState = digitalRead(SW_pin2) int count=0 if (LState =0) { count++; if(count % 2){ first mode } else{ second mode }

Please post a complete program and not a snippet

I think this will work.

No, it won't. It won't even compile.

The state change detection example shows how to detect that a pin changed state - from LOW to HIGH or from HIGH to LOW. When you know that a change happened, you can then look at the current state to see whether it is HIGH (the pin changed TO high) or LOW (the pin changed TO low), and ignore the change you don't care about.

Full programm, wanted it to: Not touched print corrent mode ('0') First push will print '1' Next push will print '2' Next push will print '1' Next push will print '2' .... And so on.

any comments?

const int SW_pin = 2; // digital pin connected to SW const int SW_pin2 = 3; // digital pin connected to SW void setup() {

// Sw_pin = Push joy pinMode(SW_pin, INPUT); digitalWrite(SW_pin, LOW); pinMode(SW_pin2, INPUT); digitalWrite(SW_pin2, LOW); Serial.begin(9600); } void loop(void) {

// Switch modes int RState = digitalRead(SW_pin2);//sw2=right Joystick int count=0; printf("0"); if (RState =1) { count++; if(count % 2){ printf("1"); }

else{ printf("2"); } } }

  int count=0;

Why reset count to zero each time through loop() ?
Why are the switch pins declared as int when byte would be plenty large enough ?

Incidentally I don’t understand how anyone can program when there code is in such a mess. Here is your code Auto formatted in the IDE and in code tags when posted here (HINT)

const int SW_pin = 2; // digital pin connected to SW
const int SW_pin2 = 3; // digital pin connected to SW

void setup()
{
  // Sw_pin = Push joy
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, LOW);
  pinMode(SW_pin2, INPUT);
  digitalWrite(SW_pin2, LOW);
  Serial.begin(9600);
}

void loop(void)
{
  // Switch modes
  int RState = digitalRead(SW_pin2);//sw2=right Joystick
  int count = 0;
  printf("0");
  if (RState = 1)
  {
    count++;
    if (count % 2)
    {
      printf("1");
    }
    else
    {
      printf("2");
    }
  }
}
    if (RState =1) {

You are assigning the value 1 to RState. You are NOT comparing the value in RState to 1.

First of all, thank you for the comment I'm here to learn, the count = 0 is not longer through loop, ty for that. The pins are declared as int , beacuse a bad habbit of "int it all, cuz why not", will change it to byte. oh, and I changed this: "if(RState =1)" to this: "if(RState ==1)" WORKS! thank you guys

UKHeliBob: Why are the switch pins declared as int when byte would be plenty large enough ?

i would use bool, sure byte and bool are the same size but bool actually signifies what they are.

Deva_Rishi: i would use bool, sure byte and bool are the same size but bool actually signifies what they are.

How do you plan to get pin numbers 2 and 3 into a bool?

Steve

Oops sorry i thought pin states... it is easy to read over the top of things,by the way if it is a constant int & byte get optimized to the minimum size required don't they ?