Only send data when a switch changes

Hi!

I want the arduino only to send data when necesarry instead of that annoying stream of 0’s and 1’s if creating a switch.
My test is a very simple one but i can’t get it to work. I have the pin 13 as an input. So when the input is in LOW, the arduino sends a 0 only once. It doesn’t send anything else unless the switch is turned to HIGH. In that case, the arduino sends a 1 only once. Until the switch is turned to LOW again.

I hope you catch the idea.

The proble i have is that suddenly i get lines of serial output like “0101100”.

Here’s the code:

int actualvalue;
int prevvalue;

void setup (){
  Serial.begin (9600);
  pinMode (13, INPUT);
  prevvalue = digitalRead (13);
}

void loop () {
  actualvalue = digitalRead (13);
  if (valactual != prevvalue) {
    Serial.print (actualvalue);
    prevvalue = actualvalue;
  }
}

Better don't use pin 13 as input.
Second, look how to write debounce code:
http://arduino.cc/playground/Main/InterfacingWithHardware#Switches

Pin 13 has the LED connected, so it's less than ideal.

Next, you should check debounce code.

Lastly, you could use byte or char instead of int and save two memory bytes. (you could mess around a bit more and save an extra byte... but it's not worth it).

The proble i have is that suddenly i get lines of serial output like "0101100".

Not with the code you posted you don't.

Why don't you post your actual code?