Serial Print RE

Hi, I am very new to programming, and I just have a quick question:

In a very simple program I am making, whenever I press a pushbutton, the arduino prints out the number one. However, when I do this action, it prints out 1 repeatedly for a random number of times. How do you solve this program?

Here is my code:

int ledPin = 13; 
int inPin = 2; 
int a = 1;   
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(inPin, INPUT);  
  Serial.begin(9600);
}
void loop(){
  val = digitalRead(inPin);  
  if (val == HIGH) { 
  digitalWrite(ledPin, LOW);
  delay(1000);// turn LED OFF
  Serial.print(a);

}
 else {
  digitalWrite(ledPin, HIGH);
  delay(1000);
  
  }
}

Can you please show how you have the switch wired? I think the input pin is floating (no pull up or pull down resistor) and retains the high state.

The best way to use these push button switches is to wire diagonally across the switch with one wire to the input pin and the other wire to ground. Set the pinMode to INPUT_PULLUP. The input will read HIGH when the switch is not pressed and LOW when pressed.

Also take a peek at the topic of "debounce a switch"

Hello Cattledog, thank you for your reply. I have posted the schematic for my project(you may need to download Fritzing to open the schematic). If you cannot open it- I have one wire attached to pin 2 and another set to 5V.

I cannot really use INPUT_PULLUP because of the fact that the Arduino really sends the data to an android application through an HC-05 module.

I think your problem is that you test for high and not for a statechange. The debounce example (already mentioned) and the state change detection example that come with the IDE can give you ideas.

I don't see any attachment, but it sounds like you have 5v across the switch with no external pull down and the input pin will be floating when the switch is not pressed.

I don't understand the constraint to using INPUT_PULLUP, but if that is the case, run a 10K resistor from the pin 2 input to ground. This will connect the input to ground when the switch is not pressed.

With the delays in your sketch, I do not think you are suffering from bounce. If you are holding the button down for more than a second you will get multiple prints.

MananShukla:
I have posted the schematic for my project(you may need to download Fritzing to open the schematic).

As a general comment don't expect anyone to download anything to help you. If you have a picture, for example, just post it as a .png or .jpg file.

More specifically, most Fritzing diagrams that we see here are useless. A photo of a simple pencil drawing is normally a lot easier to understand even if you are a very poor artist.

...R

I cannot really use INPUT_PULLUP because of the fact that the Arduino really sends the data to an android application through an HC-05 module.

That is nonsense. What the Arduino does with the data, after it reads the state of the switch, has NOTHING to do with how you wire the switch. Wake up and smell the coffee.