digitalRead returns special characters

#include <Keyboard.h>

#define caps A4
#define Q A3
#define W A2
#define E A1
#define C 6

#define seven 13
#define eight 12
#define nine 11
#define rightArrow 7
#define plus 10

#define enter 9
#define tab 8
#define Up_arrow A5
#define Down_arrow A6

bool flag = true;

void setup() {
   // put your setup code here, to run once:
  // PINMODE OUTPUT
  Keyboard.begin();

  delay(1000);

  pinMode(caps, INPUT);
  pinMode(Q, INPUT);
  pinMode(W, INPUT);
  pinMode(E, INPUT);
  pinMode(C, INPUT);

  pinMode(seven, INPUT);
  pinMode(eight, INPUT);
  pinMode(nine, INPUT);
  pinMode(rightArrow, INPUT);
  pinMode(plus, INPUT);

  pinMode(enter, INPUT);
  pinMode(tab, INPUT);

  pinMode(Up_arrow, INPUT);
  pinMode(Down_arrow, INPUT);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  //LEFT KEYS

  Serial.write(digitalRead(W));
}

This is the code and serial monitor results.

I'm trying to create a device that replaces keyboard input, but the desired value is not displayed.

I would appreciate it if you could tell me how to solve it.

Welcome to the forum

Do you know the difference between what Serial.write() does and what Serial.print() does

Try both in your sketch

digitalRead() returns 0 or 1, both are no display characters '0' or '1'.
Use Serial.print() instead.

From which device or port do you want to read and where to write characters?

I want DigitalRead to return 0 or 1. However, this code returns unknown special characters.

It is returning 0 or 1 but they are unprintable, hence the odd output

If you want to see the 0 or 1 as characters then use Serial.print() instead of Serial.write() as previously suggested

1 Like

You could also add a 0x30 Hex or a ASCII '0' to the result and you will get a 0 or 1.

dont know if this is relevant
but you may want to use

pinMode(C, INPUT_PULLUP);

because floating input can switch with noise for no reason

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.