Firstly i am very inexperienced in programming but trying my best.
I am having problems with trying to display a single can bus id data on a lcd.
This is to test an can industrial laser time of flight device.
I have worked out how to turn the laser on , and when i look at the serial monitor the received data is sensible and correct, however the data on the lcd is changing with angle and distance but the figures are not what is on the serial monitor.
Any help would be appreciated Thanks
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int R1, R2, R3, R4, R5, R6, R7, R8;
long RPM = 0;
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
#define CAN_2515
// #define CAN_2518FD
// Set SPI CS Pin according to your hardware
#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
// For Wio Terminal w/ MCP2518FD RPi Hat:
// Channel 0 SPI_CS Pin: BCM 8
// Channel 1 SPI_CS Pin: BCM 7
// Interupt Pin: BCM25
const int SPI_CS_PIN = BCM8;
const int CAN_INT_PIN = BCM25;
#else
// For Arduino MCP2515 Hat:
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
#endif
#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif
#ifdef CAN_2515
#include "mcp2515_can.h"
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif
void setup() {
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.clear();
SERIAL_PORT_MONITOR.begin(115200);
while (!Serial) {};
while (CAN_OK != CAN.begin(CAN_125KBPS)) { // init can bus : baudrate = 500k
SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
delay(100);
}
SERIAL_PORT_MONITOR.println("CAN init ok!");
}
unsigned char stmp[8] = { 0, 255, 255, 0, 0, 0, 0, 0 };
void loop() {
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
stmp[7] = stmp[7] + 1;
if (stmp[7] == 100) {
stmp[7] = 0;
stmp[6] = stmp[6] + 1;
if (stmp[6] == 100) {
stmp[6] = 0;
stmp[5] = stmp[5] + 1;
}
}
CAN.sendMsgBuf(0xAB, 0, 8, stmp);
delay(100); // send data per 100ms
SERIAL_PORT_MONITOR.println("CAN BUS sendMsgBuf ok!");
unsigned char len = 4;
unsigned char buf[8];
int a = 0;
char b = 0;
if (CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned char canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.println("get data from ID: ");
Serial.println(canId);
for (int i = 0; i < len; i++) // print the data
{
Serial.print(buf[i]);
Serial.print("\t");
}
}
b = buf[0];
R1 = int(b);
b = buf[1];
R2 = int(b);
b = buf[2];
R3 = int(b);
b = buf[3];
R4 = int(b);
b = buf[4];
R5 = int(b);
b = buf[7];
R6 = int(b);
lcd.setCursor(0, 0);
lcd.print("ANGLE ");
lcd.setCursor(7, 0);
lcd.print(R1);
lcd.setCursor(0, 1);
lcd.print("DISTANCE ");
lcd.setCursor(10, 1);
lcd.print(R3);
lcd.setCursor(0, 2);
delay(100);
}
// END FILE