Here is what i want to do. I want to use the only one ID to send CAN BUS message. When i press the button it should send it to can message like for switch one i want to do something like A0 and switch two something like B0 etc. If all the buttons are pressed then do FF. Can someone help me with this please. I am using Aurduino Uno R3 and MCP2515.
#include <SPI.h>
#include <mcp2515.h>
//CAN FRAME FOR EACH BUTTON
struct can_frame canMsg0;
MCP2515 mcp2515(10);
//Defining the buttons and their respective pin numbers
#define button1Pin 3 // D3
#define button2Pin 4 // D4
#define button3Pin 5 // D5
#define button4Pin 6 // D6
#define button5Pin 7 // D7
#define button6Pin 8 // D8
#define button7Pin 9 // D9
#define button8Pin 14 // A0
#define button9Pin 15 // A1
#define button10Pin 16 // A2
#define button11Pin 17 // A3
#define button12Pin 18 // A4
#define button13Pin 19 // A5
//defining the can with baudrate and clock speed for configuration
#define MCP_8MHz_500kBPS_CFG1 (0x00)
#define MCP_8MHz_500kBPS_CFG2 (0x90)
#define MCP_8MHz_500kBPS_CFG3 (0x82)
//We start with the state low for all the buttons
int buttonOneState = 0;
int buttonTwoState = 0;
int buttonThreeState = 0;
int buttonFourState = 0;
int buttonFiveState = 0;
int buttonSixState = 0;
int buttonSevenState = 0;
int buttonEightState = 0;
int buttonNineState = 0;
int buttonTenState = 0;
int buttonElevenState = 0;
int buttonTwelveState = 0;
int buttonThirteenState = 0;
int buttonForteenState = 0;
void setup() {
// Can bus with id, dlc and data to be transferring
canMsg0.can_id = 0x036;
canMsg0.can_dlc = 1;
while (!Serial);
Serial.begin(500000);
//Initializing the SPI
SPI.begin();
mcp2515.reset();
//Initializing the CAN protocol with 500k baudrate and 8MHz clock
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ);
//Starting the CAN in normal mode
mcp2515.setNormalMode();
//Making all pins as input due to using pull down resistor
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button4Pin, INPUT);
pinMode(button5Pin, INPUT);
pinMode(button6Pin, INPUT);
pinMode(button7Pin, INPUT);
pinMode(button8Pin, INPUT);
pinMode(button9Pin, INPUT);
pinMode(button10Pin, INPUT);
pinMode(button11Pin, INPUT);
pinMode(button12Pin, INPUT);
pinMode(button13Pin, INPUT);
}
void loop() {
// read the state of the pushbutton value
byte buttonOneState = digitalRead(button1Pin);
byte buttonTwoState = digitalRead(button2Pin);
byte buttonThreeState = digitalRead(button3Pin);
byte buttonFourState = digitalRead(button4Pin);
byte buttonFiveState = digitalRead(button5Pin);
byte buttonSixState = digitalRead(button6Pin);
byte buttonSevenState = digitalRead(button7Pin);
byte buttonEightState = digitalRead(button8Pin);
byte buttonNineState = digitalRead(button9Pin);
byte buttonTenState = digitalRead(button10Pin);
byte buttonElevenState = digitalRead(button11Pin);
byte buttonTwelveState = digitalRead(button12Pin);
byte buttonThirteenState = digitalRead(button13Pin);
// Show the state of pushbutton on serial monitor
Serial.println (buttonOneState);
Serial.println (buttonTwoState);
Serial.println (buttonThreeState);
Serial.println (buttonFourState);
Serial.println (buttonFiveState);
Serial.println (buttonSixState);
Serial.println (buttonSevenState);
Serial.println (buttonEightState);
Serial.println (buttonNineState);
Serial.println (buttonTenState);
Serial.println (buttonElevenState);
Serial.println (buttonTwelveState);
Serial.println (buttonThirteenState);
if (buttonOneState == HIGH)
{
Serial.println("Button One is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonTwoState == HIGH)
{
Serial.println("Button Two is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonThreeState == HIGH)
{
Serial.println("Button Three is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonFourState == HIGH)
{
Serial.println("Button Four is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonFiveState == HIGH)
{
Serial.println("Button Five is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonSixState == HIGH)
{
Serial.println("Button Six is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonSevenState == HIGH)
{
Serial.println("Button Seven is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonEightState == HIGH)
{
Serial.println("Button Eight is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonNineState == HIGH)
{
Serial.println("Button Nine is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonTenState == HIGH)
{
Serial.println("Button Ten is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonElevenState == HIGH)
{
Serial.println("Button Eleven is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonTwelveState == HIGH)
{
Serial.println("Button Twelve is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
if (buttonThirteenState == HIGH)
{
Serial.println("Button Thirteen is Pressed");
mcp2515.sendMessage(&canMsg0);
}
else
{
mcp2515.sendMessage(&canMsg0);
}
delay(100);
}