So I am trying to use the TX of the master Arduino to talk to the RX of the Slave Arduino. I am wanting to have a master with 8 buttons and when a button is pressed it sends a string to the slave Arduino, and the slave Arduino reads the incoming to a string, and depending on the string it will send/type a keystroke. When I uploaded the code initially it didn't work but I added Serial1 and now they can communicate. The problem is when I plug in the slave Arduino to a pc and power on the master it just repeats keystrokes 12345678 all at once every second or so. So before I even get to touch a pin to the ground ie (button press) it is sending all keystrokes at once and touching pins 1-8 to the ground doesn't visibly do anything. In short, I want to press 1 button out of 8 on the Master and have the Slave do the one action out of 8 and then wait for another keypress to send another action. Any help would be amazing.
MASTER and Slave are ATmega32u4
TX of master plugged into RX of slave
Buttons connected to digital pins 2-9 on the master Arduino sharing common ground.
Master Code
/*
*/
// This section Defines the serial string variables.
char Str1[3] = "B1";
char Str2[3] = "B2";
char Str3[3] = "B3";
char Str4[3] = "B4";
char Str5[3] = "B5";
char Str6[3] = "B6";
char Str7[3] = "B7";
char Str8[3] = "B8";
const int buttonPin[] = {2, 3, 4, 5, 6, 7, 8, 9};
int pinCount = 8;
int buttonState[] = {0, 0, 0, 0,0 ,0 ,0 ,0};
int prevButtonState[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
long lastDebounceTime[] = {0, 0, 0, 0, 0, 0, 0, 0};
long debounceDelay = 150;
void setup() {
setPrescaler();
// This is required for the use of thisPin and defines what pin the buttons are.
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
pinMode(buttonPin[thisPin], INPUT);
digitalWrite(buttonPin[thisPin], HIGH);
}
// This is important because it tells the serial to began transmitting on the TX pin.
Serial.begin(9600);
Serial1.begin(9600);
}
// Output actions. This will send a string "B1-8" to be written in the serial.
int outputAction(int currentButton) {
if (currentButton + 1 == 1) {
Serial1.write(Str1,2);
Serial.write(Str1,2);
wait(5);
}
if (currentButton + 1 == 2) {
Serial1.write(Str2,2);
Serial.write(Str2,2);
wait(5);
}
if (currentButton + 1 == 3) {
Serial1.write(Str3,2);
Serial.write(Str3,2);
wait(5);
}
if (currentButton + 1 == 4) {
Serial1.write(Str4,2);
Serial.write(Str4,2);
wait(5);
}
if (currentButton + 1 == 5) {
Serial1.write(Str5,2);
Serial.write(Str5,2);
wait(5);
}
if (currentButton + 1 == 6) {
Serial1.write(Str6,2);
Serial.write(Str6,2);
wait(5);
}
if (currentButton + 1 == 7) {
Serial1.write(Str7,2);
Serial.write(Str7,2);
wait(5);
}
if (currentButton + 1 == 8) {
Serial1.write(Str8,2);
Serial.write(Str8,2);
wait(5);
}
}
void loop() { // Main Function - workflow is called within loop();
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
buttonState[thisPin] = digitalRead(buttonPin[thisPin]);
//This is the code to tell when a button is pressed do action x defined up above @~98
if ((buttonState[thisPin] != prevButtonState[thisPin]) && (buttonState[thisPin] == HIGH)) {
if ((millis() - lastDebounceTime[thisPin]) > debounceDelay) {
outputAction(thisPin);
lastDebounceTime[thisPin] = millis();
}
}
prevButtonState[thisPin] = buttonState[thisPin];
}
}
void bootLoop() {
// digitalWrite(RXLED, LOW); // set the LED on
TXLED0; //TX LED is not tied to a normally controlled pin
delay(200); // wait for a second
TXLED1;
delay(200);
TXLED0; //TX LED is not tied to a normally controlled pin
delay(200); // wait for a second
TXLED1;
delay(800);
}
void wait(int cycles) {
for (int i = 0; i < cycles; i++) {
if (slowMode) {
delay(250);
}
}
}
void Button() {
}
void setPrescaler() {
// Disable interrupts.
uint8_t oldSREG = SREG;
cli();
// Enable change.
CLKPR = _BV(CLKPCE); // write the CLKPCE bit to one and all the other to zero
// Change clock division.
CLKPR = 0x0; // write the CLKPS0..3 bits while writing the CLKPE bit to zero
// Recopy interrupt register.
SREG = oldSREG;
}
//TODO:
#define in_developer_mode 0 // Set to 1 if device is in developer mode
Slave Code
/*
*/
#include <Keyboard.h>
#define slowMode 1
// Special characters definition
#define KEY_LEFT_CTRL 0x80
#define KEY_LEFT_SHIFT 0x81
#define KEY_LEFT_ALT 0x82
#define KEY_RIGHT_CTRL 0x84
#define KEY_RIGHT_SHIFT 0x85
#define KEY_RIGHT_ALT 0x86
#define KEY_UP_ARROW 0xDA
#define KEY_DOWN_ARROW 0xD9
#define KEY_LEFT_ARROW 0xD8
#define KEY_RIGHT_ARROW 0xD7
#define KEY_BACKSPACE 0xB2
#define KEY_TAB 0xB3
#define KEY_ENTER 0xB0
#define KEY_ESC 0xB1
#define KEY_CAPS_LOCK 0xC1
// This section Defines the serial string variables.
char IncommingButton[3];
char Str1[3] = "B1";
char Str2[3] = "B2";
char Str3[3] = "B3";
char Str4[3] = "B4";
char Str5[3] = "B5";
char Str6[3] = "B6";
char Str7[3] = "B7";
char Str8[3] = "B8";
void setup() {
setPrescaler();
Keyboard.begin();
// This is important because it tells the serial to began receiving on the RX pin.
Serial.begin(9600);
Serial1.begin(9600); // I added this one because Arduino IDE was using regular Serial
}
void loop() { // Main Function - workflow is called within loop();
Serial1.readBytes(IncommingButton,2);
if (IncommingButton == Str1, 2) {
Keyboard.print("1");
Keyboard.releaseAll();
}
if (IncommingButton == Str2, 2) {
Keyboard.print("2");
Keyboard.releaseAll();
}
if (IncommingButton == Str3, 2) {
Keyboard.print("3");
Keyboard.releaseAll();
}
if (IncommingButton == Str4, 2) {
Keyboard.print("4");
Keyboard.releaseAll();
}
if (IncommingButton == Str5, 2) {
Keyboard.print("5");
Keyboard.releaseAll();
}
if (IncommingButton == Str6, 2) {
Keyboard.print("6");
Keyboard.releaseAll();
}
if (IncommingButton == Str7, 2) {
Keyboard.print("7");
Keyboard.releaseAll();
}
if (IncommingButton == Str8, 2) {
Keyboard.print("8");
Keyboard.releaseAll();
}
}
void bootLoop() {
// digitalWrite(RXLED, LOW); // set the LED on
TXLED0; //TX LED is not tied to a normally controlled pin
delay(200); // wait for a second
TXLED1;
delay(200);
TXLED0; //TX LED is not tied to a normally controlled pin
delay(200); // wait for a second
TXLED1;
delay(800);
}
void repeatKey(byte key, int num) {
for (int i = 0; i < num; i++) {
Keyboard.write(key);
wait(1);
}
}
void wait(int cycles) {
for (int i = 0; i < cycles; i++) {
if (slowMode) {
delay(250);
}
}
}
void setPrescaler() {
// Disable interrupts.
uint8_t oldSREG = SREG;
cli();
// Enable change.
CLKPR = _BV(CLKPCE); // write the CLKPCE bit to one and all the other to zero
// Change clock division.
CLKPR = 0x0; // write the CLKPS0..3 bits while writing the CLKPE bit to zero
// Recopy interrupt register.
SREG = oldSREG;
}