Programming problem involving arrays

Hallo,

I am using a Arduino CAN BUS shield connected to a motion controller connected to a servo motor.

I can start and shut down the motor but i am struggling to change the speed.

I want to insert my own speed selection but if i do, for example i choose 60 the serial read back will go from 54 for a 6, 48 for a 0. So the speed decreases and does not stay constant.

The program is attached.

// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];

MCP_CAN CAN0(10); // Set CS to pin 10

int val;

void setup()
{
Serial.begin(115200);

// init can bus, baudrate: 500k
if(CAN0.begin(CAN_500KBPS) == CAN_OK)
{
Serial.print(“CAN OK!!\r\n”);
}
else Serial.print(“Can init fail!!\r\n”);

pinMode(2, INPUT);

Serial.write(“Please enter speed? (cnt/sec)\n”);
delay(3000);

}

unsigned char id[4] = {130, 127, 0, 0};
unsigned char sx[4] = {1, 127, 0, 0};

unsigned char mo_on[8] = {77, 79, 0, 0, 1, 0, 0, 0};
unsigned char mo_off[8] = {77, 79, 0, 0, 0, 0, 0, 0};

unsigned char bg[4] = {66, 71, 0, 0};
unsigned char st[4] = {83, 84, 0, 0};
//unsigned char jv[8] = {74, 86, 0, 0, 0, val, 0, 0};

void loop()
{
// send data: id = 0x00, standrad flame, data len = 8, stmp: data buf

int val = 0;
val = Serial.read();
val = byte(val);
Serial.print(val);
Serial.print(’\n’);

unsigned char jv[8] = {74, 86, 0, 0, 0, val, 0, 0};

CAN0.sendMsgBuf(0, 0, 4, id); // Ask id
delay(1000);

CAN0.readMsgBuf(&len, rxBuf); // Display status
rxId = CAN0.getCanId();
Serial.print("ID: “);
Serial.print(rxId, DEC);
Serial.print(” Data: ");

for(int i = 0; i<len; i++)
{
if(rxBuf *< 0x10) *

  • {*
  • Serial.print(“0”);*
  • }*
    _ Serial.print(rxBuf*, DEC);_
    _
    Serial.print(" ");_
    _
    }*_

* Serial.println();*
* delay(1000);*

* CAN0.sendMsgBuf(0, 0, 4, sx); // System on*
* delay(1000);*

* CAN0.readMsgBuf(&len, rxBuf); // Display status*
* rxId = CAN0.getCanId(); *
* Serial.print("ID: ");*
* Serial.print(rxId, DEC);*
* Serial.print(" Data: ");*

* for(int i = 0; i<len; i++) *
* {*
_ if(rxBuf < 0x10)
{

* Serial.print(“0”);
}
Serial.print(rxBuf, DEC);
Serial.print(" ");
}*_

* Serial.println();*
* delay(1000);*

* CAN0.sendMsgBuf(895, 0, 8, mo_on); // Start motor*
* delay(2000);*

* CAN0.readMsgBuf(&len, rxBuf); // Display status*
* rxId = CAN0.getCanId(); *
* Serial.print("ID: ");*
* Serial.print(rxId, DEC);*
* Serial.print(" Data: ");*

* for(int i = 0; i<len; i++) *
* {*
_ if(rxBuf < 0x10)
{

* Serial.print(“0”);
}
Serial.print(rxBuf, DEC);
Serial.print(" ");
}*_

* Serial.println();*
* delay(1000);*

* CAN0.sendMsgBuf(895, 0, 8, jv); // Spoed*
* delay(1000);*

* CAN0.readMsgBuf(&len, rxBuf); // Display status*
* rxId = CAN0.getCanId(); *
* Serial.print("ID: ");*
* Serial.print(rxId, DEC);*
* Serial.print(" Data: ");*
* Serial.print(jv[5], DEC);*

* for(int i = 0; i<len; i++) *
* {*
_ if(rxBuf < 0x10)
{

* Serial.print(“0”);
}
Serial.print(rxBuf, DEC);
Serial.print(" ");
}*_

* Serial.println();*
* delay(1000);*

* CAN0.sendMsgBuf(895, 0, 4, bg); // Begin*
* delay(5000);*

* CAN0.readMsgBuf(&len, rxBuf); // Display status*
* rxId = CAN0.getCanId(); *
* Serial.print("ID: ");*
* Serial.print(rxId, DEC);*
* Serial.print(" Data: ");*

* for(int i = 0; i<len; i++) *
* {*
_ if(rxBuf < 0x10)
{

* Serial.print(“0”);
}
Serial.print(rxBuf, DEC);
Serial.print(" ");
}*_

* Serial.println();*
* delay(1000);*

* CAN0.sendMsgBuf(895, 0, 4, st); // STOP*
* delay(2000);*

* CAN0.readMsgBuf(&len, rxBuf); // Display status*
* rxId = CAN0.getCanId(); *
* Serial.print("ID: ");*
* Serial.print(rxId, DEC);*
* Serial.print(" Data: ");*

* for(int i = 0; i<len; i++) *
* {*
_ if(rxBuf < 0x10)
{

* Serial.print(“0”);
}
Serial.print(rxBuf, DEC);
Serial.print(" ");
}*_

* Serial.println();*
* delay(1000);*

* CAN0.sendMsgBuf(895, 0, 8, mo_off); // Motor off*
* delay(2000);*

* CAN0.readMsgBuf(&len, rxBuf); // Display status*
* rxId = CAN0.getCanId(); *
* Serial.print("ID: ");*
* Serial.print(rxId, DEC);*
* Serial.print(" Data: ");*

* for(int i = 0; i<len; i++) *
* {*
_ if(rxBuf < 0x10)
{

* Serial.print(“0”);
}
Serial.print(rxBuf, DEC);
Serial.print(" ");
}*_

* Serial.println();*
* delay(1000);*
}
/*********************************************************************************************************
* END FILE*
*********************************************************************************************************/
Please help
Thank you.

Can we have the code in code tags please to avoid all the italics

But [ i ] like italics!

I want to insert my own speed selection but if i do, for example i choose 60 the serial read back will go from 54 for a 6, 48 for a 0. So the speed decreases and does not stay constant.

You are obviously not sending, or reading, serial data correctly. You need to use some end of packet marker (carriage return is fine) and store the serial data in an array until the end of packet marker arrives. When it does, NULL terminate the array and use atoi() to convert the char array to an int.

Following from @PaulS's comment the examples in serial input basics may be useful

Your first line says "connected to a servo motor" but I see no reference to a servo in the code.
However I have not studied the code carefully because of the inconvenient way it is presented.

...R

Hi,

Sorry if i posted the code the wrong way. The Arduino is the brain of the operation it tell the motion controller what to do and it will then respond back. You won’t see anything of the servo motor because the motion controller controlles that through your Arduino commands.

I tried this and it seems to work.

if(Serial.available() >= 0)
{
int val = Serial.parseInt();

if(val <= 256)
{
val = 0 + constrain(val,0,255);
jv[5] = val;
Serial.print(val);
Serial.print(’\n’);
}
else if(val <= 510 && val >= 257)
{
jv[5]= (val - 255);
jv[6]= 1;
Serial.print(val);
Serial.print(’\n’);
}
else if(val >= 511)
{
jv[5]= (val - (2*255));
jv[6]= 2;
Serial.print(val);
Serial.print(’\n’);
}
else
{
val = 0;
Serial.print(val);
Serial.print(’\n’);
}
}

Thank you