I'm currently reading 3 limit switches and sending those data over Serial.write using the following code.
#include <SPI.h>
#include <Wire.h>
#include <arduino-timer.h>
//Define limit switch pins
#define LEFT_SWITCH_PIN 5
#define RIGHT_SWITCH_PIN 6
#define REAR_SWITCH_PIN 7
bool left_switch_state;
bool right_switch_state;
bool rear_switch_state;
bool switch_state_array[3] = {left_switch_state,right_switch_state,rear_switch_state};
int switch_pin_array[3] = {LEFT_SWITCH_PIN,RIGHT_SWITCH_PIN,REAR_SWITCH_PIN};
int send_status_state = 0;
void setup() {
Wire.begin();
Serial.begin(115200);
status_timer.every(200, sendStatus);
}
void sendStatus() {
if (send_status_state == 0) {
getLimitSwitch();
unsigned char buf[5];
buf[0] = 'S';
buf[1] = switch_state_array[0];
buf[2] = switch_state_array[1];
buf[3] = switch_state_array[2];
buf[4] = '|';
Serial.write(buf,5);
send_status_state = 1;
}
}
void loop() {
status_timer.tick();
}
void getLimitSwitch()
{
for (int i=0; i<3; i++)
{
switch_state_array[i] = digitalRead(switch_pin_array[i]);
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
as you can see I'm currently using 3 bytes to send the states of each limit switch separately.
buf[1] = switch_state_array[0];
buf[2] = switch_state_array[1];
buf[3] = switch_state_array[2];
How can I send all limit switch data in one byte?