The led doesn't want to light (potentiometer /arduino /led /canbus mcp2515 )

hi i'm attempting to write a code to make led's brightness using a potentiometer
with the pot being in the sender arduino and the led being on the receiver arduino and connecting them with canbus mcp2515
diagram below


(i know it's has 2 led and potentiometer but i wanted to try one then up to 2 later (one didn't even wana work))
irl wiring

codes
sender (potentiometer)

#include <SPI.h>          
#include <mcp2515.h>      
#define potPin A0
struct can_frame canMsg;   
MCP2515 mcp2515(10);  
void setup() 
{
  while (!Serial);
  Serial.begin(9600);
  SPI.begin();   
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); 
  mcp2515.setNormalMode();
  canMsg.can_id = 0xAA; 
  canMsg.can_dlc = 2;               
}

void loop() 
{  
 int potValue = analogRead(potPin);
 int x = potValue/4;
  canMsg.data[0] = x; 
  canMsg.data[1] = 0x00;
  mcp2515.sendMessage(&canMsg); 
  delay(200);  
}

receiver

#include <SPI.h>
#include <mcp2515.h>
#include <Wire.h>
struct can_frame canMsg;
int led=2;
MCP2515 mcp2515(10);
void setup() {
  pinMode(led, OUTPUT);
  SPI.begin();
  mcp2515.reset();
  mcp2515.setBitrate(CAN_125KBPS,MCP_8MHZ);
  mcp2515.setNormalMode();
    

}
void loop() {
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
     if(canMsg.can_id==0xAA){
       int x = canMsg.data[0];
       Serial.println(x);
         analogWrite(led,x);
       delay(200);
      }   
    }
}

this is the library of mcp2515 i'm using

thank you in advance

I answered a similar question earlier today. This circuit will not work as presented, it is the same as the previous one with the pots and LEDs added. Spend some time following CAN tutorials so you understand what makes it work and what the PHY (Physical) layer looks like. Get a copy of the Arduino cookbook and skim that paying special attention to the section on CAN.

Have you tried the example codes?

If you want to know what is going on in your code add serial debug printing

Sender code with debug printing

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *

unsigned long MyTestTimer = 0;                   // Timer-variables MUST be of type unsigned long
const byte    OnBoard_LED = 13;   // onboard-LED uno, mega


#include <SPI.h>
#include <mcp2515.h>
#define potPin A0
struct can_frame canMsg;
MCP2515 mcp2515(10);
void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start Sender");
  PrintFileNameDateTime();

  SPI.begin();
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
  canMsg.can_id = 0xAA;
  canMsg.can_dlc = 2;
}


void loop()  {
  BlinkHeartBeatLED(OnBoard_LED, 250);

  int potValue = analogRead(potPin);

  int x = potValue / 4;
  canMsg.data[0] = x;
  canMsg.data[1] = 0x00;

  if ( TimePeriodIsOver(MyTestTimer, 1000) ) {
    mcp2515.sendMessage(&canMsg);
    //delay(200);
  }
}

// helper-functions
void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );
}


// easy to use helper-function for non-blocking timing
// explanation see here
// https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}



void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);

  if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
    digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
  }
}

receiver-code with debug-printing

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *

unsigned long MyTestTimer = 0;                   // Timer-variables MUST be of type unsigned long
const byte    OnBoard_LED = 13;   // onboard-LED uno, mega

/*
  if ( TimePeriodIsOver(MyTestTimer,1000) ) {

  }

*/

#include <SPI.h>
#include <mcp2515.h>
#include <Wire.h>
struct can_frame canMsg;
const byte led = 2;
MCP2515 mcp2515(10);

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start receiver");
  PrintFileNameDateTime();

  pinMode(led, OUTPUT);
  SPI.begin();
  mcp2515.reset();
  mcp2515.setBitrate(CAN_125KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
}


void loop() {
  BlinkHeartBeatLED(OnBoard_LED, 250);
  byte rcvStatus;
  int x;
  rcvStatus = mcp2515.readMessage(&canMsg);
  dbgc("01", rcvStatus);

  if (rcvStatus == MCP2515::ERROR_OK) {
    dbg("02", canMsg.can_id);

    if (canMsg.can_id == 0xAA) {
      x = canMsg.data[0];
      dbg("03", x);
      //Serial.println(x);
      analogWrite(led, x);
      delay(200);
    }
  }
}

// helper-functions
void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );
}


// easy to use helper-function for non-blocking timing
// explanation see here
// https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}



void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);

  if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
    digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
  }
}

The sender and receiver need matching baud rates.
Currently you have
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ);
mcp2515.setBitrate(CAN_125KBPS,MCP_8MHZ);

1 Like

ahh.. yeah
well it kinda worked (it didn't gradually fade it's goes from a 100 to a0 nothing in between , it just light up and light off)

Move you LED to a PWM capable pin.

a7

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.