I am having an issue when using some digital INPUT pins to send a message.
Regardless of what I make HIGH, the digitalRead if's go random (see picture)
I am jumping Pin7 to Pin2,3,4,5 to set Relays though CAN communication. However it sends the wrong data, because my I/O is not working.
In the picture, all should be OFF.
#include <mcp_can.h>
#include <SPI.h>
#include <stdio.h>
MCP_CAN CAN0(10); // Set CS to pin 10
void setup()
{
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
Serial.begin(115200);
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
else Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}
byte data[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
byte sndStat;
void loop()
{
// send data: ID = 14ffffb0, Extended CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
if(digitalRead(2) == HIGH){
data[3] = 0x01;
sndStat = CAN0.sendMsgBuf(0x14ffffb0, 1, 8, data);
Serial.println("Sent R1");
}
else if(digitalRead(3) == HIGH){
data[3] = 0x02;
sndStat = CAN0.sendMsgBuf(0x14ffffb0, 1, 8, data);
Serial.println("Sent R2");
}
else if(digitalRead(4) == HIGH){
data[3] = 0x04;
sndStat = CAN0.sendMsgBuf(0x14ffffb0, 1, 8, data);
Serial.println("Sent R3");
}
else if(digitalRead(5) == HIGH){
data[3] = 0x08;
sndStat = CAN0.sendMsgBuf(0x14ffffb0, 1, 8, data);
Serial.println("Sent R4");
}
else{
data[3] = 0x00;
sndStat = CAN0.sendMsgBuf(0x14ffffb0, 1, 8, data);
Serial.println("Sent ALL OFF");
}
delay(1000);
/*if(sndStat == CAN_OK){
Serial.println("Message Sent Successfully!");
} else {
Serial.println("Error Sending Message...");
}*/
}