Trouble with Button Inputs.....

Hello All

After finally finishing writing the code for my program, I have started testing it and have come across something really friggin' annoying.

For some reason, button inputs won't work.

I have tried to troubleshoot it, but even with the example code for Button that comes with the Arduino software, I still cannot get a response.

const int topfloatswitchpin = 24;

int floatswitchState = 0;

floatswitchState = digitalRead(topfloatswitchpin);
  
   lcd.clear();
checkwater: 
   if (floatswitchState == LOW) {
     digitalWrite(solenoidwaterinpin, HIGH);
     lcd.setCursor(0, 0);
     lcd.print("********************");
     lcd.setCursor(0, 1);
     lcd.print("**FILLING TOP TANK**");
     lcd.setCursor(0, 2);
     lcd.print("***TO 21.5 LITRES***");
     lcd.setCursor(0, 3);
     lcd.print("********************");
   }
   
   else {
     digitalWrite(solenoidwaterinpin, HIGH);
     lcd.clear();
     lcd.setCursor(0, 0);
     lcd.print("********************");
     lcd.setCursor(0, 1);
     lcd.print("**FILLING COMPLETE**");
     lcd.setCursor(0, 2);
     lcd.print("**PREHEATING WATER**");
     lcd.setCursor(0, 3);
     lcd.print("********************");
     delay(5000);
     goto heaterout;
     }
   goto checkwater;
//Opening Water Fill Valve, then checking for complete fill with float switch     
     
heaterout:

Essentially, its to fill a container with water. The container has a float switch. The water in line has a 12v solenoid valve. The valve will be controlled by means of a relay, I've just been testing it with an LED. If the pin is low, it keeps filling with water and I've used the goto function to check the switch again until it goes HIGH. It just won't go high, no matter how hard I try. (Obviously when it does go high, it uses the goto function again to go to the next stage.

Granted, I haven't got a tact switch, just using a couple of wires instead.

Now, I have it all wired up exactly as it says on the website, with the 10k resistor. I even checked the resistor colour codes

Every time I try to get this to work, it fails. It won't read the pin as HIGH. Even with the example program, it just won't work.

Im using an Arduino Mega2560 if this makes any difference at all, but it just won't work and I am tearing my hair out.

Please, can anyone point me in the right direction?

Regards,

Justin

It helps if we can see all of the code...

It won’t allow me to put in the full amount of code as it is over the 9000 character limit.

But the code I think for my project isn't 100% relevant. Even for the basic Button example, not even that works.

When you write a post, at the bottom of the input textbox is "Attachments and other options" which can be used to attach large files.

I've used the goto function to check the switch again until it goes HIGH. It just won't go high, no matter how hard I try. (Obviously when it does go high, it uses the goto function again to go to the next stage.

Ugh. Nasty, nasty gotos. Just say NO.

How is the "switch" wired ? Do you have a pullup or pulldown resistor connected to it ? What pinMode() have you used for it ?

Please post your whole program and consider getting rid of those gotos

juzztf: But the code I think for my project isn't 100% relevant. Even for the basic Button example, not even that works.

Give us a complete sketch of something that you have tried that does not work. If that is the basic button code, then post that complete sketch here.

It is very, very common for people to say "here is the bit of code you need" when in fact, the problem is outside the bit of code they gave us. Give us the complete sketch of something simple you have tried that does not work.

Hopefully this will help
Thanks

Justin

CodeforBeer.pdf (81.3 KB)

/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// 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 =  12;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

This is the basic button code I’m trying to test
I changed the led pin to Pin 12 just to I wasn’t using the internal one.

OK, the Brown goes from 5v to the loose grey "switch" wire Orange is another loose "switch" wire which goes into a 10k Resistor The yellow wire comes out of the 10k Resistor into Pin 2 The Green wire goes to ground

The Purple Wire goes from Pin 12 to the LED The Blue Wire goes from the led to Ground

I know I don't have a resistor with the led, but in earlier tests, that didn't seem to stop it working.

I just cannot get input pins to read high. They know when they are low, but not when they are high...

pulls out another chunk of hair

Regards

Justin

Move the yellow wire to the other end of the resistor.

Keep in mind that loop() is executing thousands of times per second. Unless you can blink in sync with that rate, you probably can't see the LED turn on and off. The easiest test is to add a delay(500); statement at the bottom of the loop so the LED can change state and stay there long enough to see it. If that works, then read the example code supplied with the IDE named Blink Without delay().

Ok, I’ve managed to rewire and I now am able to do the basic button code and have it working.

But now the switch is working, I still can’t seem to get the program to advance in the case of the one I wrote.

It works in a way.
If the input on the floatswitchpin is high, only before it begins to read it does it advance.

Any other time, it doesn’t work.

Any ideas?

Why don't you post the entire code from the one you're talking about with the float switch. It's herd to tell anything from the snippet in the OP.

Nevermind, I found where you attached it.

You've got calls to loop() inside other functions. Go read up on something called recursion. Calling loop from you're code is almost always a really really bad idea.

You've also got goto in there. It's really hard to figure out and follow where the code is really going with goto.

Those two things together make a code that is nearly impossible to debug. I would suggest you start over with this after looking for some tutorials and lessons on program flow. There's a thread here about planning and implementing a program. Search that out.