Serial.print once in a void loop?

I have a toggle switch and want it to act like a button. When the switch is on, the Serial.print, rapidly prints it and when it's off, nothing happens. But I want it to print once when the switch is on.
Thanks

int SwitchPin = 2;                 // Switch connected to digital pin 2
int val = 0;                          // variable to store the read value

void setup()
{
 pinMode(SwitchPin, INPUT);    // sets the digital pin 2 as input
 Serial.begin(9600);
}

void loop()
{  
 val = digitalRead(SwitchPin);   // read the input pin
 if (val == 1)
 {
 Serial.print("On or Off");
 }
}

void loop()
{
val = digitalRead(SwitchPin); // read the input pin

if (val == 1) // if the switch is off or on depending on your circuit

// print this until the switch is off (or on) then do absolutely nothing until
// pressed again.

{
Serial.print("On or Off");
}
}

What did you want to happen?

Weedpharma

That's not quite what I wanted it to do. Can I return val = 0 at the end of the void loop, I've tried and got nothing. Thanks away.

Don't use val as the "shall I print again" indicator, do something like this to ensure that you only print once

const byte SwitchPin = 2;                 // Switch connected to digital pin 2
byte val = 0;                          // variable to store the read value
boolean okToPrint = true;

void setup()
{
  pinMode(SwitchPin, INPUT);    // sets the digital pin 2 as input
  Serial.begin(9600);
}

void loop()
{
  val = digitalRead(SwitchPin);   // read the input pin
  if (val == HIGH && okToPrint)
  {
    Serial.print("On or Off");
    okToPrint = false;
  }
}

Note the changes I made to the variable declarations to save memory. Have you got a pulldown resistor on the switch input to hold it LOW when not activated ?

void loop()
{
val = digitalRead(SwitchPin); // read the input pin
if (val == 1 && pushed ==FALSE);

{
pushed = TRUE;
Serial.print("On");
}
}

You will then need to reset the flag at some point to enable it to print again.

You could follow with

If (digitalRead(switchPin) == 0 && pushed == TRUE);
{
pushed = FALSE;
Serial.print ("off");
}

Weedpharma

Sadly neither of the replies worked. Just to clarify, when the switch is up, I want nothing to happen, but when its flicked down, I want it to give my computer an input, like a keyboard. And when it's flick back up I want it to give the same input.

jAMDup:
Sadly neither of the replies worked. Just to clarify, when the switch is up, I want nothing to happen, but when its flicked down, I want it to give my computer an input, like a keyboard. And when it's flick back up I want it to give the same input.

Sigh... I'll try some mind reading here... you want it to register a key event whenever the switch is toggled, regardless of whether it is currently up or down?

As in... the switch is down... I click it up, it says, "hey, the switch moved". I click it back again, it says, "hey, the switch moved"?

Yes, that's correct but it only says "hey, the switch moved" once, sorry if it wasn't clear. Thank you

jAMDup:
Yes, that's correct but it only says "hey, the switch moved" once, sorry if it wasn't clear. Thank you

That's just a standard, normal debounced button.

After a little bit of research, I don't really understand. Sorry, but can someone write a small script for me.

That's just a standard, normal debounced button.

After a little bit of research, I don't really understand. Sorry, but can someone write a small script for me.

http://bfy.tw/Rhq

http://www.arduino.cc/en/Tutorial/Debounce

I just said, I've done research and don't understand it.

What part is of the example are you having trouble with?

.

All of it. I found a video which did help
This is the code

int SwitchPin = 2;
int val = 0;
boolean Toggle = false;
boolean pressed = false;

int Debounce = 0;

void setup() {
  pinMode(SwitchPin, INPUT);
  Serial.begin(9600);

}
///////////////////////
void Ntoggle()
{
  Serial.print("OFF ");
 
  
  }

/////////////////////////
void toggle()
{
  Serial.print("ON ");
  
  }


/////////////////////////////////////////////////
void loop() {
  if (digitalRead(SwitchPin)== HIGH)
  {
   Debounce++;

   }else
   {
    Debounce = 0;
    pressed = false;
    Toggle = false;

    }
/////////////////////////////////////////////////
    if (Debounce >= 5000)
    {
      pressed = true;
      
      }
/////////////////////////////////////////////////
    if (pressed == true && Toggle == false)
    {
      Toggle = true;
      toggle();
      }

}

As as result, the switch prints "On" when the lever is in the bottom state, when I flick it up, nothing happens. Thanks

Is this close?

/* 
 Debounce
 
 Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 a minimum delay between toggles to debounce the circuit (i.e. to ignore
 noise).  
 
 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached from pin 2 to +5V
 * 10K resistor attached from pin 2 to ground
 
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
 
 
 created 21 November 2006
 by David A. Mellis
 modified 30 Aug 2011
 by Limor Fried
 modified 28 Dec 2012
 by Mike Walters
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Debounce
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;        // the current state of the output pin
int buttonState;            // the current reading from the input pin
int lastButtonState = LOW;  // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() 
{
  Serial.begin(9600);

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() 
{
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) 
  {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 

  if ((millis() - lastDebounceTime) > debounceDelay) 
  {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) 
    {
      buttonState = reading;

      Serial.println("On or Off");

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) 
      {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

Nope, =/

What is it not doing?

I edited the code to suit my set up, and it's not outputting text.

You do not need the +5 volt connection on the switch as the internal pull-up resistor is enabled.
I assume the wires are soldered at the switch terminals.

Using the sketch I attached, tell us what is happening on the serial monitor when you flip the switch in one direction then the other.
.

Using my code I commented earlier:

Down position - "On" appears
Up position - Nothing when flicked up

Using the Arduino Debounce script:
Nothing.

Sorry if this isn't what you asked for.