Hello!
This is my first post in the forum and also my first hardware/Arduino project so bare with me. I'll try to explain it as well as I can.
The project:
I have made an Arduino Mega controller with 8 pots(Alp) and a single 128X64 OLED display. Everything is connected and works on the hardware side. I send data from the Arduino pots to Pure Data, which works. And I also send some strings from Pure Data to Arduino and then parse them on the Arduino to display them on the OLED. Ar first I had the OLED connected to 5v along the pots, but that created some pot jitter. So I moved the display to 3.3v, that removed the pot jitter. Is that okay to d that? Or maybe it's part of the issue I am having. I need to test that tommorow, now that I think of it.
But on the software side, I am having another issue.
I can load the code for the pots and the code for the display individually and they work fine.
But when I add the code for both pots and OLED to the same sketch, the OLED is not updated with the incoming string values, only the pots are working.
Been trying all day and I think I could use some tips here. I am not a programmer, just a hobbyist so any tips are appreciated
I tried changing the baudrate both up and down and in some cases nothing works. At really baudrate, like 230400, which I think is the highest the Pure Data Comport object accepts it updates the display but it happens very very slowly. But since I can't set the baudrate higher than 230400 I think I need to find another way.
I also tried moving the pot code after the OLED code and that doesn't change anything.
Is there any obvious mistakes here that I could change?
Thanks in advance
/*********************************************************************
Alexandros Drymonits pot library - Declare
*********************************************************************/
// analog_values array size, must be constant
const int num_of_analog_pins = 8;
// digital_values array size, must be constant
const int num_of_digital_pins = 0;
// create an array to store the values of the potentiometers
int analog_values[num_of_analog_pins];
// create an array to store the values of the push-buttons
int digital_values[num_of_digital_pins];
// Old analog values for deadband
int old_analog_values[num_of_analog_pins];
/*********************************************************************
Adafruit SH1106 (Some functions not available, like for SSD1306) - Declare
*********************************************************************/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#define OLED_RESET -1
Adafruit_SH1106 display(OLED_RESET);
/*********************************************************************
Parse incoming string - Declare
*********************************************************************/
String readString; //main captured String
String param1,param2,param3,param4,param5,param6,param7,param8;
int index1,index2,index3,index4,index5,index6,index7,index8;
/*********************************************************************
Void setup
*********************************************************************/
void setup() {
// Alexandros Drymonits pot library
for(int i = 0; i < num_of_digital_pins; i++){
// pinMode((i + 2), INPUT_PULLUP);
pinMode((i + 2), INPUT); }
Serial.begin(57600); // 57600 // 38400 // 74880
// SH1106 Adafruit display library
display.begin(SH1106_SWITCHCAPVCC, 0x3C); // I2C addr 0x3C(Check with Ic2 scanner)
display.clearDisplay(); // Clear the buffer/display.
delay(500);
} // Setup end
/*********************************************************************
Void loop
*********************************************************************/
void loop() {
/*********************************************************************
Serial output to Pure Data
*********************************************************************/
// Alexandros Drymonit's pot library
// Analog pins
for(int i = 0; i < num_of_analog_pins; i++) analog_values[i] = analogRead(i);
// add some deadband
for(int i = 0; i < num_of_analog_pins; i++){
int dbRange = 5;
if (analog_values[i] < (old_analog_values[i] - dbRange) || analog_values[i] > (old_analog_values[i] + dbRange))
{ old_analog_values[i] = analog_values[i]; }
// Tag for analog params
Serial.print("Analog_values: ");
// Analog params send over serial
for(int i = 0; i < (num_of_analog_pins - 1); i++){
Serial.print(old_analog_values[i]);
Serial.print(" ");
}
Serial.println(old_analog_values[num_of_analog_pins - 1]);
}
// Digital pins
// for(int i = 0; i < num_of_digital_pins; i++) digital_values[i] = !digitalRead(i + 2); // on/off
for(int i = 0; i < num_of_digital_pins; i++) digital_values[i] = digitalRead(i + 2); // off/on
Serial.print("Digital_values: ");
for(int i = 0; i < (num_of_digital_pins - 1); i++){
Serial.print(digital_values[i]);
Serial.print(" ");
}
Serial.println(digital_values[num_of_digital_pins - 1]);
/*********************************************************************
Serial input From Pure data - To display
*********************************************************************/
display.clearDisplay(); // Clear display on every loop
// https://forum.arduino.cc/index.php?topic=336753.0
// Parse an incoming string, so you can use values seperately
// Expect a string like: 90:low:15.6:125* or 130:hi:7.2:389*
// The : is for seperating the string
// The * at the end is for stopping the process
if (Serial.available()) { // Only update when serial data is received
char c = Serial.read(); // get one byte from serial buffer
if (c == '*') { // * is the end of the string
index1 = readString.indexOf(':'); // finds location of 1. ,
param1 = readString.substring(0, index1); // captures 1. data String
index2 = readString.indexOf(':', index1+1 ); // finds location of 2. ,
param2 = readString.substring(index1+1, index2); // captures 2. data String
index3 = readString.indexOf(':', index2+1 ); // finds location of 3. ,
param3 = readString.substring(index2+1, index3); // captures 3. data String
index4 = readString.indexOf(':', index3+1 ); // finds location of 4. ,
param4 = readString.substring(index3+1, index4); // captures 4. data String
index5 = readString.indexOf(':', index4+1 ); // finds location of 5. ,
param5 = readString.substring(index4+1, index5); // captures 5. data String
index6 = readString.indexOf(':', index5+1 ); // finds location of 6. ,
param6 = readString.substring(index5+1, index6); // captures 6. data String
index7 = readString.indexOf(':', index6+1 ); // finds location of 7. ,
param7 = readString.substring(index6+1, index7); // captures 7. data String
index8 = readString.indexOf(':', index7+1 ); // finds location of 8. ,
param8 = readString.substring(index7+1, index8-index7-1); // captures 8. data String
// SH1106 Adafruit display library
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.setCursor(0,0);
display.println("ParA:"+ param1);
display.println("ParB:"+ param2);
display.println("ParC:"+ param3);
display.println("ParD:"+ param4);
display.println("ParE:"+ param5);
display.println("ParF:"+ param6);
display.println("ParG:"+ param7);
display.println("ParH:"+ param8);
delay(1); // Update every x ms
display.display(); // Update the display - do it EVERYTIME you send something to display!
readString=""; //clears variable for new input
// param1=""; param2=""; param3=""; param4=""; // Rst, not needed
}
else {
readString += c; //makes the string readString
}
}
/********************************************************************/
} // Loop end