I have:
Arduino UNO
Can controller MCP2515 / SPI
Oled with 1306 chip / on I2C
Have mostly Copy/Paste code to achieve UNO to send a string to MCP2515
and read the returned message on the CAN and print the value on my OLED.
This works fine and I get the OLED to show the message as long as my UNO is connected with USB to my computer,
(The TX-led blink every second and the CAN value updates)
But as soon as I disconnect the usb and power my UNO from Powerbank or DC-source it only updates once and then keep showing the first reed value from CAN and I have to reset or flip the power to get a new reeding.
I will upload my code here when I have made some clean up in my comments, Its probably very ugly code but it seem to work (half way at least)
With the code you posted and the wiring diagram you show, it should operate the same way whether powered by USB or via Vin. So something else is going on.
What is this 8,4V power source?
Yes it is like the USB connection to my computer simulates a serial connection and when I cut that connection it stops with all communication on SPI ..?
Yes It is 2x 20700 of good quality they can take charge at about 4A and give plenty more. I have a lot of those and I have tried with different once under this project.
8,19V on battery
8,19V on battery under load
5,024V on 5V-pin on Arduino
But I think its another problem,, I will check my code and come back, now it won't update the values unless I have serial print on and serial window open in IDE
thanks a lot for your time this far I will try some things and get back in afternoon with updated code and maybe new questions...
OK, I have noticed that the new value from the CAN-bus does not update in the serial monitor either, it is the value that I receive from the first run of the program that repeat itself even in the serial monitor.
So either the program does not call on the string 60B more than once or the saved value from the returned string 58B is just saved and does not update more than once.
I will repost the code here since I have made some changes:
#include <SPI.h>
#include <mcp2515.h>
#include <Wire.h> //HL
#include <Adafruit_GFX.h> //HL
#include <Adafruit_SSD1306.h> //HL
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
struct can_frame canMsg;
struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);
void setup() {
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
mcp2515.reset();
mcp2515.setBitrate(CAN_250KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
canMsg1.can_id = 0x60B;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x40;
canMsg1.data[1] = 0x81;
canMsg1.data[2] = 0x60;
canMsg1.data[3] = 0x00;
canMsg1.data[4] = 0x00;
canMsg1.data[5] = 0x00;
canMsg1.data[6] = 0x00;
canMsg1.data[7] = 0x00;
// Serial.println("Example: Write to CAN");
// Serial.println("ID DLC DATA"); //HL
}
void loop() {
display.clearDisplay();
//HLTiwhile (!Serial);
//Serial.begin(115200);
mcp2515.sendMessage(&canMsg1);
// mcp2515.sendMessage(&canMsg2);
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
// Serial.print(canMsg.can_id, HEX); // print ID
// Serial.print(" ");
// Serial.print(canMsg.can_dlc, HEX); // print DLC
// Serial.print(" ");
for (int i = 4; i<canMsg.can_dlc; i) { // print the data. HL ändrat 0 till 4 , tagit bort ++ efter i,
//HLOn Serial.println(canMsg.data[i],HEX); // lagt till ln efter print
//HLOn Serial.println(canMsg.data[i],DEC); // lagt till ln efter print
//Serial.print(" ");
display.display();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0); // Start at top-left corner -->middle
display.println("Batt. SoC");
display.println(" ");
display.setCursor(0,25); // Start at top-left corner -->middle
// display.setTextColor(SSD1306_WHITE);
display.print(" ");
display.setTextSize(4); // Draw 4X-scale text
display.print(canMsg.data[i],DEC);
display.println("%");
display.println(" ");
// display.display();
delay(1000); //HL 2s-->1s
}
}
}