Hello,
I´m not complete new to arduino, but in a very early stage of programming.
My project goal is to read a can message from a can bus and display the value from can on a SSD1309 oled.
Im currently using a cheap MCP2515 shield, arduino nano and 1,3" SSD1309. But in the moment only Display connected to nano.
I´m using the U8g2 and Seedstudio MCP2515 lib.
Already had the code up and running to show text and calculated numbers on the oled, and be able to switch cases with button.
But i´m a bit stuck, as i´m now want to implement the MCP2515 code.
Current goal is to receive a can message with ID 329 and read from Data Byte 4 the Bit 6. If that Bit ==1 i want to count up the "pagecount" int to switch cases.
But after implement some lines for the MCP2515, the Display won´t do anything.
Sorry for messy code, the u8g2 lib is well documentated, but with MCP2515 lib i have some problems. And of course i try to learn.
And sorry for some english mistakes, it´s not my first language.
Best regards.
#include <mcp_can.h>
#include <mcp_can_dfs.h>
#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
const byte interruptPin = 0;
unsigned char flagRecv = 0;
unsigned long ID = 0;
unsigned char len = 0;
unsigned char buf[8];
unsigned char stat = 0;
char str[20];
int pagecount = 0;
int buttonState = 0;
int set = 0;
int pagehelp = 0;
//define Display && Pins clock=SCL; data=SDA
U8G2_SSD1309_128X64_NONAME0_1_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 4, /* dc=*/ 9, /* reset=*/ 3);
void setup(void) {
Serial.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
delay(100);
u8g2.begin();
pinMode(7, INPUT_PULLUP);
digitalWrite(7, HIGH); // Für echtes High bei geöffneter Taste sorgen
}
}
void loop(void) {
//Can Lesen
if(CAN_MSGAVAIL == CAN.checkReceive()) { // Check to see whether data is read
CAN.readMsgBufID(&ID, &len, buf); // read data, len: data length, buf: data buf
if(ID == 329) {
stat = buf[4];
if (bitRead(stat, 6)){
digitalWrite(pagehelp, 1);
}
}
}
delay(10);
//Werte umschalten
if (pagecount >3){
pagecount = 1;
}
if (pagehelp == 1 && set == 0){
pagecount++;
set = 1;
}
if (pagehelp == 0 && set == 1) {
delay(250);
set=0;
}
char buf2[9];
sprintf (buf2, "%d", pagecount);
switch (pagecount) {
case 0:
u8g2.setFont(u8g2_font_freedoomr10_tu);
u8g2.firstPage();
do {
u8g2.setCursor(40, 40);
u8g2.print(F("STARTSEITE"));
u8g2.drawStr(100, 15, buf2);
} while ( u8g2.nextPage() );
delay(100);
break;
case 1:
u8g2.setFont(u8g2_font_freedoomr10_tu);
u8g2.firstPage();
do {
u8g2.setCursor(0, 25);
u8g2.print(F("PAGE2"));
u8g2.setCursor(0, 50);
u8g2.print(F("G"));
u8g2.drawStr(100, 15, buf2);
} while ( u8g2.nextPage() );
delay(100);
break;
case 2:
u8g2.setFont(u8g2_font_freedoomr10_tu);
u8g2.firstPage();
do {
u8g2.setCursor(0, 25);
u8g2.print(F("PAGE3"));
u8g2.setCursor(0, 50);
u8g2.print(F("D"));
u8g2.drawStr(100, 15, buf2);
} while ( u8g2.nextPage() );
delay(100);
break;
case 3:
u8g2.setFont(u8g2_font_freedoomr10_tu);
u8g2.firstPage();
do {
u8g2.setCursor(0, 25);
u8g2.print(F("PAGE4"));
u8g2.setCursor(0, 50);
u8g2.print(F("E"));
u8g2.drawStr(100, 15, buf2);
} while ( u8g2.nextPage() );
delay(100);
break;
default:
break;
}
}