Hello, I am desperate for an answer
im trying to send a command to the Arduino to turn a servo motor. I enter the command in CAN MONITOR, the motor turns, but when I send the command through a virtual button, nothing happens. I am uploading my codes please help me with this thing
Simple command as 0x0c80:0x0 0x32
my project
my ino code
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN 125
#define SERVOMAX 625
void setup() {
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(60);
}
void loop() {
if (Serial.available() >= 12) { // 4 bytes for serialBlockTag, 4 bytes for canFrameId, 8 bytes for frameData
byte serialBlockTag[4];
Serial.readBytes(serialBlockTag, 4);
if (serialBlockTag[0] == 0x44 && serialBlockTag[1] == 0x33 && serialBlockTag[2] == 0x22 && serialBlockTag[3] == 0x11) {
unsigned long canFrameId;
Serial.readBytes((char*)&canFrameId, 4);
if (canFrameId == 0x0c80) {
byte frameData[8];
Serial.readBytes(frameData, 8);
int combinedValue = (frameData[0] << 8) | frameData[1];
int servoNum = (combinedValue >> 8) & 0xFF;
int angle = combinedValue & 0xFF;
if (servoNum >= 0 && servoNum < 16 && angle >= 0 && angle <= 180) {
int pulseLength = map(angle, 0, 180, SERVOMIN, SERVOMAX);
pwm.setPWM(servoNum, 0, pulseLength);
Serial.print("Servo ");
Serial.print(servoNum);
Serial.print(" angle: ");
Serial.println(angle);
} else {
Serial.println("Invalid servo number or angle.");
}
} else {
Serial.println("Invalid CAN frame ID.");
}
} else {
Serial.println("Invalid serial block tag.");
}
}
}
my xml code
<RealDashCAN version="2">
<frames baseId="3200">
<frame id="3200" writeInterval="1000">
<value name="CombinedValue" units="bit" offset="0" length="1"/>
<value name="ServoNum" units="bit" offset="0" length="1"/>
<value name="Angle" units="bit" offset="0" length="1"/>
</frame>
</frames>
</RealDashCAN>
