MultiTasking Arduino Switch inputs

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);
}

And what is your problem?

Hint: if you put your button pins to an array, your code can be 12 times shorter

In general - Arrays and structs are your friends.
Don't duplicate code in your sketch. Write code once - use it multiple times.

The problem is i am not a perfect person in using can bus. what i want to do is. currently i have 0's coming on pcan view when nothing is pressed. like this 00. but i want to send button presses value on the same standard ID and when all buttons are pressed then send FF on can bus.

may be because you send 00 ?

If you need another value when the button is pressed - so send another value.

how can i do that? can you give me an example?
i am only using DLC = 1;
will it be canMsg0.data[0] = 0xA0; maybe?

can you give me an example?

1 Like

next time try it yourself rather than asking

1 Like

Thank you <3 again i am very new to can bus so i am a beginner in to this.

how would i structure the all buttons pressed ?

Do you need an option for all buttons only? And if 2 and 3 buttons are pressed at the same time - this case does not need to be noted in any way?

so i need option for all button pressed and if either of the buttons are pressed together like button 2 and button 3 are pressed together then display.

Do you able to use DLC > 1 ?

no keeping DLC = 1; is the requirement

As an option - you can add the results of reading all the buttons - if the sum is equal to the number of buttons, then all are pressed

sounds like a good plan. Thank you so much i will commment here if i have any questions.

what am i doing wrong here? without even a push can is getting a message of push button.

void setup() {

// Can bus with id, dlc and data to be transferring
  canMsg0.can_id  = 0x036;
  canMsg0.can_dlc = 1;
  canMsg0.data[0]= 0x00;
 

 
  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(millis() - run_time > 100000)
    {
        run_time = millis();
    }

    if(millis() - button_time >30)
  {

      if (buttonOneState == HIGH)   
      { 
        Serial.println("Button One is Pressed");   
        canMsg0.data[0] = 0xA0;
        mcp2515.sendMessage(&canMsg0);
      }
      else if (buttonTwoState == HIGH)
      {
        Serial.println("Button Two is Pressed");   
        canMsg0.data[0] = 0xA2;
        mcp2515.sendMessage(&canMsg0);

      }
      
      else if (buttonThreeState == HIGH)
      {
        Serial.println("Button Three is Pressed");
        canMsg0.data[0] = 0xA3;
        mcp2515.sendMessage(&canMsg0);
      }

      
      else if (buttonFourState == HIGH)
      {
        Serial.println("Button Four is Pressed");
      canMsg0.data[0] = 0xA4;
        mcp2515.sendMessage(&canMsg0); 
      }

      
      else if (buttonFiveState == HIGH)
      {
        Serial.println("Button Five is Pressed");
        canMsg0.data[0] = 0xA5;
        mcp2515.sendMessage(&canMsg0); 
      }

    
      else if (buttonSixState == HIGH)
      {
        Serial.println("Button Six is Pressed");
        canMsg0.data[0] = 0xA6;
        mcp2515.sendMessage(&canMsg0);
      }

      else if (buttonSevenState == HIGH)
      {
        Serial.println("Button Seven is Pressed");
        canMsg0.data[0] = 0xA7;
        mcp2515.sendMessage(&canMsg0);
      }

      
      else if (buttonEightState == HIGH)
      {
        Serial.println("Button Eight is Pressed");
        canMsg0.data[0] = 0xA8;
        mcp2515.sendMessage(&canMsg0);
      }

      
      else if (buttonNineState == HIGH)
      {
        Serial.println("Button Nine is Pressed");
      canMsg0.data[0] = 0xA9;
        mcp2515.sendMessage(&canMsg0);
      }

      
      else if (buttonTenState == HIGH)
      {
        Serial.println("Button Ten is Pressed");
      canMsg0.data[0] = 0xB1;
        mcp2515.sendMessage(&canMsg0);
      }

      
      else if (buttonElevenState == HIGH)
      {
        Serial.println("Button Eleven is Pressed");
      canMsg0.data[0] = 0xB2;
        mcp2515.sendMessage(&canMsg0);
      }

      
      else if (buttonTwelveState == HIGH)
      {
        Serial.println("Button Twelve is Pressed");
      canMsg0.data[0] = 0xB3;
        mcp2515.sendMessage(&canMsg0);
      }
      
      else if (buttonThirteenState == HIGH)
      {
        Serial.println("Button Thirteen is Pressed");
        canMsg0.data[0] = 0xB4;
        mcp2515.sendMessage(&canMsg0);
      }
      else 
      {
        mcp2515.sendMessage(&canMsg0);
        canMsg0.data[0] = 0x00;
      }
  }
  delay(500);
}

With which data code?

Are your buttons has pull-down resistors?

yes my buttons are pull down resistor. It should only send can message when the button is pushed.