(Solved)Pushbutton writes back d1 in serial monitor when pressed, instead of 1.

Hi.
My problem/question is all in the title. Why do I get d1 back in serial monitor and not just 1 when button is pressed? Will arduino read d1 as HIGH when i use: if (val == HIGH)?

Thanks :slight_smile:

Please post your full sketch. If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it’s ok to add it as an attachment. Don’t put your code in some external file service like dropbox, etc. We shouldn’t need to go to an external website just to help you. I do feel it’s reasonable to post a link to code hosted on GitHub or similar code hosting sites since that’s an platform specifically designed for this sort of thing

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor then you will not have access to this useful tool. I recommend using the standard Arduino IDE instead.

When your code requires a library that’s not included with the Arduino IDE please post a link (using the chain links icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

xtcypher:
Hi.
My problem/question is all in the title. Why do I get d1 back in serial monitor and not just 1 when button is pressed? Will arduino read d1 as HIGH when i use: if (val == HIGH)?

Thanks :slight_smile:

Because that's obviously what your code told it to do. These things only do exactly as they are programmed.

Maybe you have made some mistake in your program. Show your program to someone and be sure to include all the details about what board you have and what is connected to it and then maybe they will be able to help you.

What you thought anyone would be able to tell from the post you made here is beyond me. There's no context to that at all.

Hi.
Thanks guys, and I'm sorry.

The setup is:
Arduino Uno
HC-05 module
Push button

HC-05 connected to Tx to tx and rx to rx trough simple voltage divider. 5v to 5v on arduino and ground.
Push button connected to pin 7.

code master

int val;
#define button 7

void setup() {

  Serial.begin(9600);
  pinMode(button, INPUT);


}

void loop() {
  val = digitalRead(button);
  if (val == HIGH)
  {
    Serial.write(100);
    delay(500);
  }
  Serial.println(val);
  delay(50);
}

This is something I found on youtube so i copied, and it worked for that guy with arduino nano.
I'm trying to turn led on and off on other arduino uno through bluetooth.
So when I open serial monitor I get d1 when I press the button.

The other arduino setup is:
HC-06 - Tx to Rx on arduino and rx to tx on arduino trough simple voltage divider.
Led to pin 7

code slave

int val;
int pos = 0;
#define led 7

void setup() {

  Serial.begin(9600);
  pinMode(led, OUTPUT);


}

void loop() {
  if (Serial.available() > 0)
  {
    val = Serial.read();
    if (val == 100 && pos == 0)
    {
      digitalWrite(led, HIGH);
      pos = 1;
    }
    else if (val == 100 && pos == 1)
    {
      digitalWrite(led, LOW);
      pos = 0;
    }
    Serial.println(val);
    delay(10);
  }
}

Hope this helps, and I'm really sorry for the first post with no information what so ever.. :confused:

Serial.write(100) writes the binary data 100. ASCII code 100 is the letter d.

This tutorial might make things more clear:
https://www.arduino.cc/en/Tutorial.ASCIITable

Thanks.

I figured out why the led didn't go on or off...
I only swapped the tx-rx on hc-06 after AT command. So the hc-05 was still connected tx-tx and rx-rx. My bad. I'm embarrassed and will in the future go over wiring 5 more times after I'm SURE they are right.

Thank you so much for answering me on my first post. Hopefully last. And I'll remember the advice on how to post.