CAN Protocol With UART Interface HC-05 Bluetooth Module || #MIT-APP #CA...



MASTER:- 

//YOUTUBE.COM/MKINVENTIONS MADHAN CHIRUGURI
#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(10);

char command;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();

  canMsg.can_id  = 0x0F6;
  canMsg.can_dlc = 8;
}

void loop() {

if(Serial.available() > 0){
  command = Serial.read();
  Serial.print("command:");
  Serial.println(command);

  canMsg.data[0] = command ;
  mcp2515.sendMessage(&canMsg);
  delay(10);
}
}


SLAVE:-

#include <SPI.h>
#include <mcp2515.h>


int LED1 = A0;
int LED2 = A1;
int IN1 = 5;
int IN2 = 6;


struct can_frame canMsg;
MCP2515 mcp2515(10);


void setup() {
  Serial.begin(9600);
  SPI.begin();
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();

  
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
}

void loop() 
{
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) 
  {   
    if(canMsg.can_id == 0x0F6)
    {
      char command = canMsg.data[0];

      
//  Serial.print("command:");
//  Serial.println(command);

       if(command == 'X'){ 
        Serial.println("LED1 ON");
        digitalWrite(LED1, HIGH);} 
        if(command == 'x'){
        Serial.println("LED1 OFF");
        digitalWrite(LED1, LOW);}

       if(command == 'Y'){ 
        Serial.println("LED2 ON");
        digitalWrite(LED2, HIGH);} 
        if(command == 'y'){
          Serial.println("LED2 OFF");
        digitalWrite(LED2, LOW);}


      if(command == 'F'){ analogWrite(IN1, 255); analogWrite(IN2, 0);} 
      else if(command == 'B'){ analogWrite(IN1, 0); analogWrite(IN2, 255);}  
      else if(command == 'L'){ analogWrite(IN1, 255); analogWrite(IN2, 0);} 
      else if(command == 'R'){ analogWrite(IN1, 0); analogWrite(IN2, 255);}  
      else{ analogWrite(IN1, 0); analogWrite(IN2, 0); }

      
     delay(10);
    }
    
  }
}

Post a Comment

0 Comments