the task is as follows.
we have arduino nano and transmitter MCP2515
also we have 3 separate on / off switches (which should be connected to the arduino nano)
in this picture you can see my nano connection to the can bus sender
also you see free outputs from arduino nano
the code should work like this
arduino gets power but doesn't send anything !
we turn on switch number #1 and arduino starts send ( canMasg1 delay (100) )
we turn on switch number #3 and arduino start send ( canMasag3 delay (100) )
we turn off switch number #2 and arduino stop send ( canmasag2 delay (120) )
parts pic what i used
Arduino help wanted !!!
Arduino help wanted !!!
- Attachments
-
- 942155.jpg (38.27 KiB) Viewed 6715 times
Re: Arduino help wanted !!!
this cod what i have now
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);
void setup() {
canMsg1.can_id = 0x167;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x81;
canMsg1.data[1] = 0x80;
canMsg1.data[2] = 0x00;
canMsg1.data[3] = 0x01;
canMsg1.data[4] = 0xFF;
canMsg1.data[5] = 0xE0;
canMsg1.data[6] = 0x00;
canMsg1.data[7] = 0x00;
canMsg2.can_id = 0x165;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x10;
canMsg2.data[1] = 0x40;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x00;
canMsg2.data[4] = 0x00;
canMsg2.data[5] = 0x65;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0x00;
while (!Serial);
Serial.begin(115200);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate (CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("Example: Write to CAN");
}
void loop() {
mcp2515.sendMessage(&canMsg1);
Serial.println("Messages sent");
delay(10);{
mcp2515.sendMessage(&canMsg2);
Serial.println("Messages sent");
delay(0);{
}
}
}
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);
void setup() {
canMsg1.can_id = 0x167;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x81;
canMsg1.data[1] = 0x80;
canMsg1.data[2] = 0x00;
canMsg1.data[3] = 0x01;
canMsg1.data[4] = 0xFF;
canMsg1.data[5] = 0xE0;
canMsg1.data[6] = 0x00;
canMsg1.data[7] = 0x00;
canMsg2.can_id = 0x165;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x10;
canMsg2.data[1] = 0x40;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x00;
canMsg2.data[4] = 0x00;
canMsg2.data[5] = 0x65;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0x00;
while (!Serial);
Serial.begin(115200);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate (CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("Example: Write to CAN");
}
void loop() {
mcp2515.sendMessage(&canMsg1);
Serial.println("Messages sent");
delay(10);{
mcp2515.sendMessage(&canMsg2);
Serial.println("Messages sent");
delay(0);{
}
}
}
Last edited by Abricos on Sat May 15, 2021 9:04 pm, edited 3 times in total.
Re: Arduino help wanted !!!
Hello! As I understood, you can use this:
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);
volatile bool but1, but2, but3;
volatile int x, y, z;
void setup() {
canMsg1.can_id = 0x167;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x81;
canMsg1.data[1] = 0x80;
canMsg1.data[2] = 0x00;
canMsg1.data[3] = 0x01;
canMsg1.data[4] = 0xFF;
canMsg1.data[5] = 0xE0;
canMsg1.data[6] = 0x00;
canMsg1.data[7] = 0x00;
canMsg2.can_id = 0x165;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x10;
canMsg2.data[1] = 0x40;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x00;
canMsg2.data[4] = 0x00;
canMsg2.data[5] = 0x65;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0x00;
canMsg3.can_id = 0x163;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x10;
canMsg2.data[1] = 0x40;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x00;
canMsg2.data[4] = 0x00;
canMsg2.data[5] = 0x65;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0x00;
while (!Serial);
Serial.begin(115200);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate (CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("Example: Write to CAN");
pinMode(A0, INPUT_PULLUP); // button 1
pinMode(A1, INPUT_PULLUP); // button 2
pinMode(A2, INPUT_PULLUP); // button 3
}
/*void zaderzhka(){
if(but1==LOW&&but2==LOW&&but3==LOW){
x=0;
y=0;
z=0;
} else if(but1==HIGH&&but2==LOW&&but3==LOW){
x=100;
y=0;
z=0;
} else if(but1==LOW&&but2==HIGH&&but3==LOW){
x=0;
y=100;
z=0;
} else if(but1==LOW&&but2==LOW&&but3==HIGH){
x=0;
y=0;
z=120;
} else if(but1==HIGH&&but2==HIGH&&but3==LOW){
x=50;
y=50;
z=0;
} else if(but1==HIGH&&but2==LOW&&but3==HIGH){
x=20;
y=0;
z=0;
} else
} */
void loop() {
but1=digitalRead(A0);
but2=digitalRead(A1);
but3=digitalRead(A2);
//void zaderzhka();
if(but1==HIGH){
mcp2515.sendMessage(&canMsg1);
Serial.println("Messages sent");
delay(100); //you can set here delay you need of put x
}
if(but2==HIGH){
mcp2515.sendMessage(&canMsg2);
Serial.println("Messages sent");
delay(100); //you can set here delay you need or put y
}
if(but3==HIGH){
mcp2515.sendMessage(&canMsg3);
Serial.println("Messages sent");
delay(100); //you can set here delay you need or put z
}
But look, furst, you need to kill noises occur when you turn a button on or off. I would use one of those examples of connection
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);
volatile bool but1, but2, but3;
volatile int x, y, z;
void setup() {
canMsg1.can_id = 0x167;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x81;
canMsg1.data[1] = 0x80;
canMsg1.data[2] = 0x00;
canMsg1.data[3] = 0x01;
canMsg1.data[4] = 0xFF;
canMsg1.data[5] = 0xE0;
canMsg1.data[6] = 0x00;
canMsg1.data[7] = 0x00;
canMsg2.can_id = 0x165;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x10;
canMsg2.data[1] = 0x40;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x00;
canMsg2.data[4] = 0x00;
canMsg2.data[5] = 0x65;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0x00;
canMsg3.can_id = 0x163;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x10;
canMsg2.data[1] = 0x40;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x00;
canMsg2.data[4] = 0x00;
canMsg2.data[5] = 0x65;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0x00;
while (!Serial);
Serial.begin(115200);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate (CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("Example: Write to CAN");
pinMode(A0, INPUT_PULLUP); // button 1
pinMode(A1, INPUT_PULLUP); // button 2
pinMode(A2, INPUT_PULLUP); // button 3
}
/*void zaderzhka(){
if(but1==LOW&&but2==LOW&&but3==LOW){
x=0;
y=0;
z=0;
} else if(but1==HIGH&&but2==LOW&&but3==LOW){
x=100;
y=0;
z=0;
} else if(but1==LOW&&but2==HIGH&&but3==LOW){
x=0;
y=100;
z=0;
} else if(but1==LOW&&but2==LOW&&but3==HIGH){
x=0;
y=0;
z=120;
} else if(but1==HIGH&&but2==HIGH&&but3==LOW){
x=50;
y=50;
z=0;
} else if(but1==HIGH&&but2==LOW&&but3==HIGH){
x=20;
y=0;
z=0;
} else
} */
void loop() {
but1=digitalRead(A0);
but2=digitalRead(A1);
but3=digitalRead(A2);
//void zaderzhka();
if(but1==HIGH){
mcp2515.sendMessage(&canMsg1);
Serial.println("Messages sent");
delay(100); //you can set here delay you need of put x
}
if(but2==HIGH){
mcp2515.sendMessage(&canMsg2);
Serial.println("Messages sent");
delay(100); //you can set here delay you need or put y
}
if(but3==HIGH){
mcp2515.sendMessage(&canMsg3);
Serial.println("Messages sent");
delay(100); //you can set here delay you need or put z
}
But look, furst, you need to kill noises occur when you turn a button on or off. I would use one of those examples of connection
- Attachments
-
- debounce_001.png (8.08 KiB) Viewed 6656 times
Re: Arduino help wanted !!!
Also I added one function which I didn't finished and just commented. This function decides which delay it needs to use in each case.
Now if you need a hard time operation, you will not have it.
For example, if you put delays like this: 100, 100, 120, your controller will send the first message, sleep for 100 msec, then send the second message, then sleep for 100 msec and so on. If you need to make the Arduino send messages between sendings of the first message, you have to finish my zaderzhka function. Or ask me and wait.
Now if you need a hard time operation, you will not have it.
For example, if you put delays like this: 100, 100, 120, your controller will send the first message, sleep for 100 msec, then send the second message, then sleep for 100 msec and so on. If you need to make the Arduino send messages between sendings of the first message, you have to finish my zaderzhka function. Or ask me and wait.
Re: Arduino help wanted !!!
I rewrote the code and made it like-multitask.
Now you can just change the x, y, z values in the end of the Setup function and you'll have what you need. If you need a really hard timing, just put the x, y, z values little less than you need (because data transfering also requires some time).
Also now you just need to connect button pins to GND via buttons and you'll have what you need.
The code is here:
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg1;
struct can_frame canMsg2;
struct can_frame canMsg3;
MCP2515 mcp2515(10);
volatile bool but1, but2, but3;
int x, y, z;
volatile unsigned long int millisold1, millisold2, millisold3;
void setup() {
canMsg1.can_id = 0x167;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x81;
canMsg1.data[1] = 0x80;
canMsg1.data[2] = 0x00;
canMsg1.data[3] = 0x01;
canMsg1.data[4] = 0xFF;
canMsg1.data[5] = 0xE0;
canMsg1.data[6] = 0x00;
canMsg1.data[7] = 0x00;
canMsg2.can_id = 0x165;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x10;
canMsg2.data[1] = 0x40;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x00;
canMsg2.data[4] = 0x00;
canMsg2.data[5] = 0x65;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0x00;
canMsg3.can_id = 0x163;
canMsg3.can_dlc = 8;
canMsg3.data[0] = 0x10;
canMsg3.data[1] = 0x40;
canMsg3.data[2] = 0x00;
canMsg3.data[3] = 0x00;
canMsg3.data[4] = 0x00;
canMsg3.data[5] = 0x65;
canMsg3.data[6] = 0x00;
canMsg3.data[7] = 0x00;
while (!Serial);
Serial.begin(115200);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate (CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("Example: Write to CAN");
pinMode(A0, INPUT_PULLUP); // button 1
pinMode(A1, INPUT_PULLUP); // button 2
pinMode(A2, INPUT_PULLUP); // button 3
millisold1=millis();
millisold2=millis();
millisold3=millis();
x=100; //"delay" for the 1st message
y=100; //"delay" for the 2nd message
z=100; //"delay" for the 3rd message
}
void loop() {
but1=digitalRead(A0);
but2=digitalRead(A1);
but3=digitalRead(A2);
if ((millis()-x)>=millisold1 && but1==HIGH){
millisold1=millis();
mcp2515.sendMessage(&canMsg1);
Serial.println("Messages sent");
}
if((millis()-y)>=millisold2 && but2==HIGH){
millisold2=millis();
mcp2515.sendMessage(&canMsg2);
Serial.println("Messages sent");
}
if((millis()-z)>=millisold3 && but3==HIGH){
millisold3=millis();
mcp2515.sendMessage(&canMsg3);
Serial.println("Messages sent");
}
}
Now you can just change the x, y, z values in the end of the Setup function and you'll have what you need. If you need a really hard timing, just put the x, y, z values little less than you need (because data transfering also requires some time).
Also now you just need to connect button pins to GND via buttons and you'll have what you need.
The code is here:
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg1;
struct can_frame canMsg2;
struct can_frame canMsg3;
MCP2515 mcp2515(10);
volatile bool but1, but2, but3;
int x, y, z;
volatile unsigned long int millisold1, millisold2, millisold3;
void setup() {
canMsg1.can_id = 0x167;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x81;
canMsg1.data[1] = 0x80;
canMsg1.data[2] = 0x00;
canMsg1.data[3] = 0x01;
canMsg1.data[4] = 0xFF;
canMsg1.data[5] = 0xE0;
canMsg1.data[6] = 0x00;
canMsg1.data[7] = 0x00;
canMsg2.can_id = 0x165;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x10;
canMsg2.data[1] = 0x40;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x00;
canMsg2.data[4] = 0x00;
canMsg2.data[5] = 0x65;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0x00;
canMsg3.can_id = 0x163;
canMsg3.can_dlc = 8;
canMsg3.data[0] = 0x10;
canMsg3.data[1] = 0x40;
canMsg3.data[2] = 0x00;
canMsg3.data[3] = 0x00;
canMsg3.data[4] = 0x00;
canMsg3.data[5] = 0x65;
canMsg3.data[6] = 0x00;
canMsg3.data[7] = 0x00;
while (!Serial);
Serial.begin(115200);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate (CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("Example: Write to CAN");
pinMode(A0, INPUT_PULLUP); // button 1
pinMode(A1, INPUT_PULLUP); // button 2
pinMode(A2, INPUT_PULLUP); // button 3
millisold1=millis();
millisold2=millis();
millisold3=millis();
x=100; //"delay" for the 1st message
y=100; //"delay" for the 2nd message
z=100; //"delay" for the 3rd message
}
void loop() {
but1=digitalRead(A0);
but2=digitalRead(A1);
but3=digitalRead(A2);
if ((millis()-x)>=millisold1 && but1==HIGH){
millisold1=millis();
mcp2515.sendMessage(&canMsg1);
Serial.println("Messages sent");
}
if((millis()-y)>=millisold2 && but2==HIGH){
millisold2=millis();
mcp2515.sendMessage(&canMsg2);
Serial.println("Messages sent");
}
if((millis()-z)>=millisold3 && but3==HIGH){
millisold3=millis();
mcp2515.sendMessage(&canMsg3);
Serial.println("Messages sent");
}
}
Re: Arduino help wanted !!!
not tested
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg1;
struct can_frame canMsg2;
struct can_frame canMsg3;
struct can_frame canMsg4;
struct can_frame canMsg5;
struct can_frame canMsg6;
struct can_frame canMsg7;
struct can_frame canMsg8;
struct can_frame canMsg9;
struct can_frame canMsg10;
struct can_frame canMsg11;
struct can_frame canMsg12;
MCP2515 mcp2515(10);
volatile bool but1, but2, but3;
int x11, x12, x13, x14, x21, x22, x23, x24, x31, x32, x33, x34;
volatile unsigned long int millisold11, millisold21, millisold31, millisold12, millisold22, millisold32, millisold13, millisold23, millisold33, millisold14, millisold24, millisold34;
void setup() {
canMsg1.can_id = 0x167;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x81;
canMsg1.data[1] = 0x80;
canMsg1.data[2] = 0x00;
canMsg1.data[3] = 0x01;
canMsg1.data[4] = 0xFF;
canMsg1.data[5] = 0xE0;
canMsg1.data[6] = 0x00;
canMsg1.data[7] = 0x00;
canMsg2.can_id = 0x165;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x10;
canMsg2.data[1] = 0x40;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x00;
canMsg2.data[4] = 0x00;
canMsg2.data[5] = 0x65;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0x00;
canMsg3.can_id = 0x163;
canMsg3.can_dlc = 8;
canMsg3.data[0] = 0x10;
canMsg3.data[1] = 0x40;
canMsg3.data[2] = 0x00;
canMsg3.data[3] = 0x00;
canMsg3.data[4] = 0x00;
canMsg3.data[5] = 0x65;
canMsg3.data[6] = 0x00;
canMsg3.data[7] = 0x00;
canMsg4.can_id = 0x163;
canMsg4.can_dlc = 8;
canMsg4.data[0] = 0x10;
canMsg4.data[1] = 0x40;
canMsg4.data[2] = 0x00;
canMsg4.data[3] = 0x00;
canMsg4.data[4] = 0x00;
canMsg4.data[5] = 0x65;
canMsg4.data[6] = 0x00;
canMsg4.data[7] = 0x00;
canMsg4.can_id = 0x163;
canMsg5.can_dlc = 8;
canMsg5.data[0] = 0x10;
canMsg5.data[1] = 0x40;
canMsg5.data[2] = 0x00;
canMsg5.data[3] = 0x00;
canMsg5.data[4] = 0x00;
canMsg5.data[5] = 0x65;
canMsg5.data[6] = 0x00;
canMsg5.data[7] = 0x00;
canMsg6.can_id = 0x163;
canMsg6.can_dlc = 8;
canMsg6.data[0] = 0x10;
canMsg6.data[1] = 0x40;
canMsg6.data[2] = 0x00;
canMsg6.data[3] = 0x00;
canMsg6.data[4] = 0x00;
canMsg6.data[5] = 0x65;
canMsg6.data[6] = 0x00;
canMsg6.data[7] = 0x00;
canMsg7.can_id = 0x163;
canMsg7.can_dlc = 8;
canMsg7.data[0] = 0x10;
canMsg7.data[1] = 0x40;
canMsg7.data[2] = 0x00;
canMsg7.data[3] = 0x00;
canMsg7.data[4] = 0x00;
canMsg7.data[5] = 0x65;
canMsg7.data[6] = 0x00;
canMsg7.data[7] = 0x00;
canMsg8.can_id = 0x163;
canMsg8.can_id = 0x163;
canMsg8.can_dlc = 8;
canMsg8.data[0] = 0x10;
canMsg8.data[1] = 0x40;
canMsg8.data[2] = 0x00;
canMsg8.data[3] = 0x00;
canMsg8.data[4] = 0x00;
canMsg8.data[5] = 0x65;
canMsg8.data[6] = 0x00;
canMsg8.data[7] = 0x00;
canMsg9.can_id = 0x163;
canMsg9.can_dlc = 8;
canMsg9.data[0] = 0x10;
canMsg9.data[1] = 0x40;
canMsg9.data[2] = 0x00;
canMsg9.data[3] = 0x00;
canMsg9.data[4] = 0x00;
canMsg9.data[5] = 0x65;
canMsg9.data[6] = 0x00;
canMsg9.data[7] = 0x00;
canMsg10.can_id = 0x163;
canMsg10.can_dlc = 8;
canMsg10.data[0] = 0x10;
canMsg10.data[1] = 0x40;
canMsg10.data[2] = 0x00;
canMsg10.data[3] = 0x00;
canMsg10.data[4] = 0x00;
canMsg10.data[5] = 0x65;
canMsg10.data[6] = 0x00;
canMsg10.data[7] = 0x00;
canMsg11.can_id = 0x163;
canMsg11.can_dlc = 8;
canMsg11.data[0] = 0x10;
canMsg11.data[1] = 0x40;
canMsg11.data[2] = 0x00;
canMsg11.data[3] = 0x00;
canMsg11.data[4] = 0x00;
canMsg11.data[5] = 0x65;
canMsg11.data[6] = 0x00;
canMsg11.data[7] = 0x00;
canMsg12.can_id = 0x163;
canMsg12.can_dlc = 8;
canMsg12.data[0] = 0x10;
canMsg12.data[1] = 0x40;
canMsg12.data[2] = 0x00;
canMsg12.data[3] = 0x00;
canMsg12.data[4] = 0x00;
canMsg12.data[5] = 0x65;
canMsg12.data[6] = 0x00;
canMsg12.data[7] = 0x00;
while (!Serial);
Serial.begin(115200);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate (CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("Example: Write to CAN");
pinMode(A0, INPUT_PULLUP); // button 1
pinMode(A1, INPUT_PULLUP); // button 2
pinMode(A2, INPUT_PULLUP); // button 3
millisold11=millis();
millisold12=millis();
millisold13=millis();
millisold14=millis();
millisold21=millis();
millisold22=millis();
millisold23=millis();
millisold24=millis();
millisold31=millis();
millisold32=millis();
millisold33=millis();
millisold34=millis();
x11=100; //"delay" for the 1st message with 1st button
x12=100; //"delay" for the 2nd message with 1st button
x13=100; //"delay" for the 3rd message with 1st button
x14=100; //"delay" for the 4th message with 1st button
x21=100; //"delay" for the 1st message with 2nd button
x22=100; //"delay" for the 2nd message with 2nd button
x23=100; //"delay" for the 3rd message with 2nd button
x24=100; //"delay" for the 4th message with 2nd button
x31=100; //"delay" for the 1st message with 3nd button
x32=100; //"delay" for the 2nd message with 3nd button
x33=100; //"delay" for the 3rd message with 3nd button
x34=100; //"delay" for the 4th message with 3nd button
}
void loop() {
but1=digitalRead(A0);
but2=digitalRead(A1);
but3=digitalRead(A2);
if (but1==HIGH){
if((millis()-x11)>=millisold11){
millisold11=millis();
mcp2515.sendMessage(&canMsg1);
Serial.println("Message1.1 sent");
}
if((millis()-x12)>=millisold12){
millisold12=millis();
mcp2515.sendMessage(&canMsg2);
Serial.println("Message1.2 sent");
}
if((millis()-x13)>=millisold13){
millisold13=millis();
mcp2515.sendMessage(&canMsg3);
Serial.println("Message1.3 sent");
}
if((millis()-x14)>=millisold14){
millisold14=millis();
mcp2515.sendMessage(&canMsg4);
Serial.println("Message1.4 sent");
}
}
if(but2==HIGH){
if((millis()-x21)>=millisold21){
millisold21=millis();
mcp2515.sendMessage(&canMsg5);
Serial.println("Message2.1 sent");
}
if((millis()-x22)>=millisold22){
millisold22=millis();
mcp2515.sendMessage(&canMsg6);
Serial.println("Message2.2 sent");
}
if((millis()-x23)>=millisold23){
millisold23=millis();
mcp2515.sendMessage(&canMsg7);
Serial.println("Message2.3 sent");
}
if((millis()-x24)>=millisold24){
millisold24=millis();
mcp2515.sendMessage(&canMsg8);
Serial.println("Message2.4 sent");
}
}
if(but3==HIGH){
if((millis()-x31)>=millisold31){
millisold31=millis();
mcp2515.sendMessage(&canMsg9);
Serial.println("Message3.1 sent");
}
if((millis()-x32)>=millisold32){
millisold32=millis();
mcp2515.sendMessage(&canMsg10);
Serial.println("Message3.2 sent");
}
if((millis()-x33)>=millisold33){
millisold33=millis();
mcp2515.sendMessage(&canMsg11);
Serial.println("Message3.3 sent");
}
if((millis()-x34)>=millisold34){
millisold34=millis();
mcp2515.sendMessage(&canMsg12);
Serial.println("Message3.4 sent");
}
}
}
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg1;
struct can_frame canMsg2;
struct can_frame canMsg3;
struct can_frame canMsg4;
struct can_frame canMsg5;
struct can_frame canMsg6;
struct can_frame canMsg7;
struct can_frame canMsg8;
struct can_frame canMsg9;
struct can_frame canMsg10;
struct can_frame canMsg11;
struct can_frame canMsg12;
MCP2515 mcp2515(10);
volatile bool but1, but2, but3;
int x11, x12, x13, x14, x21, x22, x23, x24, x31, x32, x33, x34;
volatile unsigned long int millisold11, millisold21, millisold31, millisold12, millisold22, millisold32, millisold13, millisold23, millisold33, millisold14, millisold24, millisold34;
void setup() {
canMsg1.can_id = 0x167;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x81;
canMsg1.data[1] = 0x80;
canMsg1.data[2] = 0x00;
canMsg1.data[3] = 0x01;
canMsg1.data[4] = 0xFF;
canMsg1.data[5] = 0xE0;
canMsg1.data[6] = 0x00;
canMsg1.data[7] = 0x00;
canMsg2.can_id = 0x165;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x10;
canMsg2.data[1] = 0x40;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x00;
canMsg2.data[4] = 0x00;
canMsg2.data[5] = 0x65;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0x00;
canMsg3.can_id = 0x163;
canMsg3.can_dlc = 8;
canMsg3.data[0] = 0x10;
canMsg3.data[1] = 0x40;
canMsg3.data[2] = 0x00;
canMsg3.data[3] = 0x00;
canMsg3.data[4] = 0x00;
canMsg3.data[5] = 0x65;
canMsg3.data[6] = 0x00;
canMsg3.data[7] = 0x00;
canMsg4.can_id = 0x163;
canMsg4.can_dlc = 8;
canMsg4.data[0] = 0x10;
canMsg4.data[1] = 0x40;
canMsg4.data[2] = 0x00;
canMsg4.data[3] = 0x00;
canMsg4.data[4] = 0x00;
canMsg4.data[5] = 0x65;
canMsg4.data[6] = 0x00;
canMsg4.data[7] = 0x00;
canMsg4.can_id = 0x163;
canMsg5.can_dlc = 8;
canMsg5.data[0] = 0x10;
canMsg5.data[1] = 0x40;
canMsg5.data[2] = 0x00;
canMsg5.data[3] = 0x00;
canMsg5.data[4] = 0x00;
canMsg5.data[5] = 0x65;
canMsg5.data[6] = 0x00;
canMsg5.data[7] = 0x00;
canMsg6.can_id = 0x163;
canMsg6.can_dlc = 8;
canMsg6.data[0] = 0x10;
canMsg6.data[1] = 0x40;
canMsg6.data[2] = 0x00;
canMsg6.data[3] = 0x00;
canMsg6.data[4] = 0x00;
canMsg6.data[5] = 0x65;
canMsg6.data[6] = 0x00;
canMsg6.data[7] = 0x00;
canMsg7.can_id = 0x163;
canMsg7.can_dlc = 8;
canMsg7.data[0] = 0x10;
canMsg7.data[1] = 0x40;
canMsg7.data[2] = 0x00;
canMsg7.data[3] = 0x00;
canMsg7.data[4] = 0x00;
canMsg7.data[5] = 0x65;
canMsg7.data[6] = 0x00;
canMsg7.data[7] = 0x00;
canMsg8.can_id = 0x163;
canMsg8.can_id = 0x163;
canMsg8.can_dlc = 8;
canMsg8.data[0] = 0x10;
canMsg8.data[1] = 0x40;
canMsg8.data[2] = 0x00;
canMsg8.data[3] = 0x00;
canMsg8.data[4] = 0x00;
canMsg8.data[5] = 0x65;
canMsg8.data[6] = 0x00;
canMsg8.data[7] = 0x00;
canMsg9.can_id = 0x163;
canMsg9.can_dlc = 8;
canMsg9.data[0] = 0x10;
canMsg9.data[1] = 0x40;
canMsg9.data[2] = 0x00;
canMsg9.data[3] = 0x00;
canMsg9.data[4] = 0x00;
canMsg9.data[5] = 0x65;
canMsg9.data[6] = 0x00;
canMsg9.data[7] = 0x00;
canMsg10.can_id = 0x163;
canMsg10.can_dlc = 8;
canMsg10.data[0] = 0x10;
canMsg10.data[1] = 0x40;
canMsg10.data[2] = 0x00;
canMsg10.data[3] = 0x00;
canMsg10.data[4] = 0x00;
canMsg10.data[5] = 0x65;
canMsg10.data[6] = 0x00;
canMsg10.data[7] = 0x00;
canMsg11.can_id = 0x163;
canMsg11.can_dlc = 8;
canMsg11.data[0] = 0x10;
canMsg11.data[1] = 0x40;
canMsg11.data[2] = 0x00;
canMsg11.data[3] = 0x00;
canMsg11.data[4] = 0x00;
canMsg11.data[5] = 0x65;
canMsg11.data[6] = 0x00;
canMsg11.data[7] = 0x00;
canMsg12.can_id = 0x163;
canMsg12.can_dlc = 8;
canMsg12.data[0] = 0x10;
canMsg12.data[1] = 0x40;
canMsg12.data[2] = 0x00;
canMsg12.data[3] = 0x00;
canMsg12.data[4] = 0x00;
canMsg12.data[5] = 0x65;
canMsg12.data[6] = 0x00;
canMsg12.data[7] = 0x00;
while (!Serial);
Serial.begin(115200);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate (CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("Example: Write to CAN");
pinMode(A0, INPUT_PULLUP); // button 1
pinMode(A1, INPUT_PULLUP); // button 2
pinMode(A2, INPUT_PULLUP); // button 3
millisold11=millis();
millisold12=millis();
millisold13=millis();
millisold14=millis();
millisold21=millis();
millisold22=millis();
millisold23=millis();
millisold24=millis();
millisold31=millis();
millisold32=millis();
millisold33=millis();
millisold34=millis();
x11=100; //"delay" for the 1st message with 1st button
x12=100; //"delay" for the 2nd message with 1st button
x13=100; //"delay" for the 3rd message with 1st button
x14=100; //"delay" for the 4th message with 1st button
x21=100; //"delay" for the 1st message with 2nd button
x22=100; //"delay" for the 2nd message with 2nd button
x23=100; //"delay" for the 3rd message with 2nd button
x24=100; //"delay" for the 4th message with 2nd button
x31=100; //"delay" for the 1st message with 3nd button
x32=100; //"delay" for the 2nd message with 3nd button
x33=100; //"delay" for the 3rd message with 3nd button
x34=100; //"delay" for the 4th message with 3nd button
}
void loop() {
but1=digitalRead(A0);
but2=digitalRead(A1);
but3=digitalRead(A2);
if (but1==HIGH){
if((millis()-x11)>=millisold11){
millisold11=millis();
mcp2515.sendMessage(&canMsg1);
Serial.println("Message1.1 sent");
}
if((millis()-x12)>=millisold12){
millisold12=millis();
mcp2515.sendMessage(&canMsg2);
Serial.println("Message1.2 sent");
}
if((millis()-x13)>=millisold13){
millisold13=millis();
mcp2515.sendMessage(&canMsg3);
Serial.println("Message1.3 sent");
}
if((millis()-x14)>=millisold14){
millisold14=millis();
mcp2515.sendMessage(&canMsg4);
Serial.println("Message1.4 sent");
}
}
if(but2==HIGH){
if((millis()-x21)>=millisold21){
millisold21=millis();
mcp2515.sendMessage(&canMsg5);
Serial.println("Message2.1 sent");
}
if((millis()-x22)>=millisold22){
millisold22=millis();
mcp2515.sendMessage(&canMsg6);
Serial.println("Message2.2 sent");
}
if((millis()-x23)>=millisold23){
millisold23=millis();
mcp2515.sendMessage(&canMsg7);
Serial.println("Message2.3 sent");
}
if((millis()-x24)>=millisold24){
millisold24=millis();
mcp2515.sendMessage(&canMsg8);
Serial.println("Message2.4 sent");
}
}
if(but3==HIGH){
if((millis()-x31)>=millisold31){
millisold31=millis();
mcp2515.sendMessage(&canMsg9);
Serial.println("Message3.1 sent");
}
if((millis()-x32)>=millisold32){
millisold32=millis();
mcp2515.sendMessage(&canMsg10);
Serial.println("Message3.2 sent");
}
if((millis()-x33)>=millisold33){
millisold33=millis();
mcp2515.sendMessage(&canMsg11);
Serial.println("Message3.3 sent");
}
if((millis()-x34)>=millisold34){
millisold34=millis();
mcp2515.sendMessage(&canMsg12);
Serial.println("Message3.4 sent");
}
}
}