Problem with INPUT

I have Arduino Mega connected to Adafruit Feather Huzzah ESP8266.

I’m trying to send INPUT signal from Mega into Feather.
INPUT pin on Feather is 16. It’s 3.3V input coming from Mega. It supposed to trigger Feather to run //something

so far I got into problem with reading INPUT levels.

Here is my code for Feather:

int buttonPin = 16;
int buttonState;

String A = "Button State = ";
String B = A + buttonState;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  delay(15);
}

void loop() {

  buttonState = digitalRead(buttonPin);
  delay(15);

  if (buttonState < 1) {
    delay(1000);
    Serial.println("We are in < 1 statement");
    Serial.println(B);
    delay(1000);
  }
  if (buttonState >= 1) {
    delay(1000);
    Serial.println("We are in >= 1 statement");
    Serial.println(B);
    delay(1000);
  }
}

The problem with that function is, when I apply 3.3V to PIN 16 on Feather
it changes from

1st If Statement - Serial.println(“We are in < 1 statement”);

to 2nd If Statement - Serial.println(“We are in >= 1 statement”);

but it constantly displays Button State = 0

It should change to 1 right? Why is it constalty diplaying 0?

This is what I keep getting on Serial Monitor:

We are in < 1 statement
Button State = 0
We are in < 1 statement
Button State = 0
We are in < 1 statement
Button State = 0
We are in >= 1 statement
Button State = 0
We are in >= 1 statement
Button State = 0
We are in >= 1 statement
Button State = 0

Thank you.

Are the Feather inputs 5V tolerant?

String B = A + buttonState;

This is the only place that you add buttonState to A (B is not changed anywhere else) so what do you expect Serial.println(B); to output?

  if (buttonState >= 1) {

Greater than? The digitalRead() function returns HIGH or LOW.

There is no value greater than HIGH. So, you don't look to bright writing code to anticipate one.

What, exactly, is connected to the buttonPin? It isn't a button, is it?

Post a complete schematic.

Connecting a 5V Mega output to a 3.3V feather pin is not a great idea.

PaulS:

  if (buttonState >= 1) {

Greater than? The digitalRead() function returns HIGH or LOW.

There is no value greater than HIGH. So, you don't look to bright writing code to anticipate one.

What, exactly, is connected to the buttonPin? It isn't a button, is it?

Post a complete schematic.

Connecting a 5V Mega output to a 3.3V feather pin is not a great idea.

I'm not connecting to 5V output from Mega but 3.3V output. I know I would burn board with 5V and yes I'm not too bright, I'm working my way around for weeks, step by step.
I'm planning on using Level Shifter to change 5V to 3.3V but so far I'm only triggering
PIN 13 on feather using 3.3V output from Mega, its temporary setup.

By the way, a lot of mistakes I get from other websites.

So, you have a constant 3.3V fed into the feather pin. Then, it should always read HIGH.

PaulS, I wrote another code example, I’m still working on same problem.

Here, when you upload the code to Feather, PIN14 is instantly HIGH and it turns LED ON and it prints “LED HIGH” on my Serial Monitor. Why is it HIGH? I didn’t apply any 3.3V onto PIN16.

I want it to work like a switch, when you plug in Feather to Power I want the LED to stay OFF, when I apply 3.3V to PIN16 I want PIN16 to go HIGH and Turn LED ON on PIN14, however, at the second I release 3.3V from PIN16 I want LED to turn OFF.

What am I missing?

int buttonPin = 16;
int buttonState = 0;
int LED = 14;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(LED, OUTPUT);
  delay(15);
}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState < 1) {
    digitalWrite(LED, LOW);
    Serial.println("LED LOW");
    delay(15);
  }
  if (buttonState >= 1) {
    LED_f();
    delay(15);
  }
}

void LED_f() {
  digitalWrite(LED, HIGH);
  Serial.println("LED HIGH");
  delay(15);
}

Thank you.

VXfnAvqe:
Here, when you upload the code to Feather, PIN14 is instantly HIGH and it turns LED ON and it prints "LED HIGH" on my Serial Monitor. Why is it HIGH? I didn't apply any 3.3V onto PIN16.

A floating input can be any value. Connect it to ground to test 'not 3.3V'.

MorganS:
A floating input can be any value. Connect it to ground to test ‘not 3.3V’.

MorganS, I got another way around with INPUT_PULLUP, the floating state, I reversed the function and seem to work except one thing. How do I bring PIN16 to LOW/ Turn OFF LED after I release 3.3V from PIN16?

As of now, when I release 3.3v LED stays ON and I have to apply the voltage again to turn off the LED. I don’t want it to work as a button, more like a momentary switch. Its ON when you hold it, goes OFF when you let go.

int buttonPin = 16;
int buttonState = 0;
int LED = 14;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  delay(15);
}

void loop() {

  buttonState = !digitalRead(buttonPin);

  if (buttonState < 1) {
    digitalWrite(LED, LOW);
    Serial.println("LED LOW");
    delay(15);
  }
  if (buttonState >= 1) {
    LED_f();
    delay(15);
  }
}

void LED_f() {
  digitalWrite(LED, HIGH);
  Serial.println("LED HIGH");
  delay(15);
}
digitalWrite(LED, buttonState);

MorganS it doesn't work because buttonState doesn't change upon release. It stays HIGH with INPUT_PULLUP or just INPUT. It only changes its value when I apply voltage to PIN16 for 2nd time.

It just doesn't make any sense. Such a simple thing doesn't work.
It should change its state upon release but it's not.....

it works like this now:

0.0v -->> PIN16 -->> LED OFF
3.3v -->> PIN16 -->> LED ON
0.0v -->> PIN16 -->> LED ON (IT SHOULD CHANGE IT STATE TO LOW AT THIS POINT)
3.3v -->> PIN16 -->> LED OFF (DOESN'T LOOK RIGHT :frowning:

Where exactly did you put my one-line fix?

Show a complete example with setup() and loop() which compiles and shows the problem.

  if (buttonState < 1) {
    digitalWrite(LED, LOW);
    Serial.println("LED LOW");
    delay(15);
  }
  if (buttonState >= 1) {

It STILL doesn’t make sense to be using less than or greater than for states that can only equal HIGH or LOW.

Try this, with appropriate code in place of the comments:

if(buttonState == HIGH)
{
   // Turn the LED off
}
else
{
   // Turn the LED on
}

By the way, it doesn’t make sense to create a function to turn the LED off unless you create a similar one to turn it on. Of course, when you do that, you’ll see that the only difference between the functions is the state to apply to the pin. So, you’d make a single function that took an argument. Then, if you needed to do the same with a different pin, you’d need a new function. Or another argument. When you added the argument, you’d discover that you are doing nothing but calling digitalRead() with the two arguments, so you’d delete the whole function, and just call digitalRead().