instead of attempting at 2 bytes im going for four bytes as a fix on this at 4 bytes should solve most of my issues
Problem im incurring is on button press of button2 the correct information Wire.write ('B2BH'); or the depress of that button is not getting sent to the slave in a way the slave can understand it.
im trying to figure out how to get the correct 4 bytes recognized by the slave.
Sketch Master
/* Master Arduino code (Leonardo)
uses 2 Arduino's
1 set as master the other set as slave more less to double I/O pins
Master collects digital input button toggles and sends them to slave
via Wire.write using I2C method
Slave acts as the output indicator controlling multiple LEDS that indicate
which buttons on the master are toggled on or off
Also in addition the Master functions as a Keyboard relaying a keystroke
When the switches are togggled from either position.
*/
#include <Wire.h>
const int button1Pin = 4; // pin 4 is not the I2C bus for the Leonardo
const int button2Pin = 5;
const int JoyDown1ThrottlePin = 8;
const int JoyUp1ThrottlePin = 9;
int previousJoyUp1ThrottleState = HIGH;
int previousJoyDown1ThrottleState = HIGH;
int previousButton1State = HIGH; // for checking the state of button1
int previousButton2State = HIGH; // for checking the state of button2
void setup() {
Serial.begin (9600);
//while(!Serial); // wait for the serial monitor for the Leonardo
Serial.println(F("Master started"));
Keyboard.begin (); // only available in the Leonardo
pinMode (button1Pin, INPUT_PULLUP);
pinMode (button2Pin, INPUT_PULLUP);
pinMode (JoyUp1ThrottlePin, INPUT_PULLUP);
pinMode (JoyDown1ThrottlePin, INPUT_PULLUP);
Wire.begin();
// put your setup code here, to run once:
}
void loop() {
// Start of Joy1UpThrottle
int JoyUp1State = digitalRead(JoyUp1ThrottlePin);
// check if the button state has changed.
if (JoyUp1State != previousJoyUp1ThrottleState) {
if( JoyUp1State == HIGH) {
Serial.println ("JoyUpThrottleAxisCentered");
//Wire.beginTransmission(5); //REMoved not controlling a LED here
//Wire.write ('S'); //REMoved not controlling a LED here
//Wire.endTransmission(); //REMoved not controlling a LED here
//Keyboard.release (KEY_LEFT_SHIFT);
}
else {
Serial.println ("Joy1UpThrottleUp");
//Wire.beginTransmission(5); //REMoved not controlling a LED here
//Wire.write ('W'); //REMoved not controlling a LED here
//Wire.endTransmission(); //REMoved not controlling a LED here
//Keyboard.press (KEY_LEFT_SHIFT);
}
previousJoyUp1ThrottleState = JoyUp1State;
}
// End of JoyUp1Throttle
// Start of Joy1DownThrottle
int JoyDown1State = digitalRead(JoyDown1ThrottlePin);
// check if the button state has changed.
if (JoyDown1State != previousJoyDown1ThrottleState) {
if( JoyDown1State == HIGH) {
Serial.println ("Joy_Y_Axis_Centered");
//Wire.beginTransmission(5); //REMoved not controlling a LED here
//Wire.write ('S'); //REMoved not controlling a LED here
//Wire.endTransmission(); //REMoved not controlling a LED here
//Keyboard.release (KEY_LEFT_CTRL);
}
else {
Serial.println ("Joy_Y_Axis_+");
//Wire.beginTransmission(5); //REMoved not controlling a LED here
//Wire.write ('W'); //REMoved not controlling a LED here
//Wire.endTransmission(); //REMoved not controlling a LED here
//Keyboard.press (KEY_LEFT_CTRL);
}
previousJoyDown1ThrottleState = JoyDown1State;
}
// End of JoyDown1Throttle
// Start of Button1
int button1State = digitalRead(button1Pin);
// check if the button state has changed.
if (button1State != previousButton1State) {
if( button1State == HIGH) {
Serial.println ("Button1Released");
Wire.beginTransmission(5); // Assign a destination
Wire.write ('L'); // Queing into memory to send 1 byte
Wire.endTransmission(); //Transmit the byte
//Keyboard.print ('Later_Defined');
}
else {
Serial.println ("Button1Pressed");
Wire.beginTransmission(5); // Assign a destination
Wire.write ('H'); // Queing into memory to send 1 byte
Wire.endTransmission(); //Transmit the byte
//Keyboard.print ('Later_Defined');
}
previousButton1State = button1State;
}
// End of Button1
delay(50); // slow down loop
// Start of Button2
int button2State = digitalRead(button2Pin);
// check if the button state has changed.
if (button2State != previousButton2State) {
if( button2State == HIGH) {
Serial.println ("Button2Released");
Wire.beginTransmission(5); // Assign a destination
Wire.write ('B2BL'); // Attempt at Queing 4 bytes
Wire.endTransmission(); //Transmit the bytes
//Keyboard.print ('Later_Defined');
}
else {
Serial.println ("Button2Pressed");
Wire.beginTransmission(5); // Assign a destination
Wire.write ('B2BH'); // Attempt at Queing 4 bytes
Wire.endTransmission(); //Transmit the bytes
//Keyboard.print ('Later_Defined');
}
previousButton2State = button2State;
}
// End of Button2
}
Sketch Slave
/* SLAVE Arduino CODE (MEGA2560)
uses 2 Arduino's
1 set as master the other set as slave more less to double I/O pins
Master collects digital input button toggles and sends them to slave
via Wire.write using I2C method
Slave acts as the output indicator controlling multiple LEDS that indicate
which buttons on the master are toggled on or off
Also in addition the Master functions as a Keyboard relaying a keystroke
When the switches are togggled from either position.
*/
#include <Wire.h>
const int button1LedGreen = 13;
const int button1LedRed = 12;
const int button2LedGreen = 11;
const int button2LedRed = 10;
void setup()
{
Serial.begin(9600);
//Serial.println(F("Slave started"));
Serial.println ("Slave Started");
pinMode(button1LedGreen, OUTPUT); // output for LED1 from toggle Button 1 ON
pinMode(button1LedRed, OUTPUT); // output for LED2 from toggle Button 1 OFF
pinMode(button2LedGreen, OUTPUT); // output for LED2 from toggle Button 2 ON
pinMode(button2LedRed, OUTPUT); // output for LED2 from toggle Button 2 OFF
digitalWrite (button1LedGreen, LOW);
digitalWrite (button1LedRed, LOW);
digitalWrite (button2LedGreen, LOW);
digitalWrite (button1LedRed, LOW);
Wire.begin(5);
Wire.onReceive(receiveEvent);
}
void loop()
{
}
void receiveEvent(int howMany)
{
// start of receive one byte
// we are expecting just a single byte
if (howMany == 1)
{
char c = Wire.read();
// Start of LED1 for Button1
if (c == 'H')
{
digitalWrite(button1LedGreen, HIGH);
digitalWrite(button1LedRed, LOW);
Serial.print (c);
}
else if (c == 'L')
{
digitalWrite(button1LedRed, HIGH);
digitalWrite(button1LedGreen, LOW);
// End of LED1 for Button1
}
Serial.println (c);
//End of receive 1 byte
}
// My attempt to receive 4 bytes
if (howMany == 4)
{
char d = Wire.read();
Serial.println (d);
// Start of LED2 for Button2
if (d == 'B2BH')
{
digitalWrite(button2LedGreen, HIGH);
digitalWrite(button2LedRed, LOW);
}
else if (d == 'B2BL')
{
digitalWrite(button2LedRed, HIGH);
digitalWrite(button2LedGreen, LOW);
// End of LED1 for Button1
}
Serial.println (d);
//End of Attempt at Four bytes received
}
}