Arduino UNO + Software vs Hardware Serial Connection with alphanumeric OLED problem

I try build a PC monitor what using alphanumeric OLED. This display have dual function PC monitor + Spectrum analyser. PC monitor using OpenHardwarMonitor serial communication via python script, using Arduino UNO USB hardware serial port, Spectrum analyser using Winamp spectrum analyser plugin for connection this using USB serial adapter and get data with software serial connection.

I have two switch for both function pin 4 and 5. Code working well, but i have a bug or i have forget something, because after switch from software serial to hardver serial - from PC stat mode to Spectrum analyser mode and back, screen only working still Winamp runs, hardware serial conenction is active.

If im closing Winamp, PC stat remains ,,blank" if im start again Winamp PC stat mode works.

Software serial issue, buffering issue or i have forget something?

  #include <Adafruit_CharacterOLED.h>
  #include <SoftwareSerial.h>
  
  SoftwareSerial mySerial(3, 2); // RX, TX
  Adafruit_CharacterOLED lcd(OLED_V2, 6, 7, 8, 9, 10, 11, 12);
  byte v1[8] = {0, 0, 0, 0, 0, 0, 0, 31};
  byte v2[8] = {0, 0, 0, 0, 0, 0, 31, 31};
  byte v3[8] = {0, 0, 0, 0, 0, 0, 31, 31};
  byte v4[8] = {0, 0, 0, 0, 31, 0, 31, 31};
  byte v5[8] = {0, 0, 0, 31, 31, 0, 31, 31};
  byte v6[8] = {0, 0, 0, 31, 31, 0, 31, 31};
  byte v7[8] = {0, 31, 0, 31, 31, 0, 31, 31};
  byte v8[8] = {31, 31, 0, 31, 31, 0, 31, 31};
        
  int buttonPin = 4;
  int buttonPin2 = 5;
  
  byte serial_getch() {
    while (Serial.available() == 0);
    return Serial.read();
  }
   
  void setup()
  {
  
  Serial.begin(19200);
  mySerial.begin(9600);
    
  lcd.begin(16, 2);
  
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, LOW);     // Initializing
  digitalWrite(buttonPin, HIGH);    // button pin is HIGH, so it drops to 0 if pressed
  
  pinMode(buttonPin2, INPUT);
  digitalWrite(buttonPin2, LOW);     // Initializing
  digitalWrite(buttonPin2, HIGH);    // button pin is HIGH, so it drops to 0 if pressed
  
  
  lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);
  
  }
  
  void loop()
  {
  
  if(digitalRead(buttonPin)==LOW)
  
  {
  
  
  String s = mySerial.readString();
  
  lcd.clear();
       
      if (s.length() <= 16){
      lcd.setCursor(0, 0);
      lcd.print(s);
      }
      else {
        
        lcd.setCursor(0,0);
        lcd.print(s.substring(0,16));
        lcd.setCursor(0,1);
        lcd.print(s.substring(16, s.length())); 
      }  
  
            
  }
  
  if(digitalRead(buttonPin2)==LOW){    
  
     
    byte rxbyte = serial_getch();
   
    if (rxbyte == 'j')
    {
      byte r1 = serial_getch();
      byte r2 = serial_getch();
      lcd.setCursor(r1 - '0', r2 - '0');
      lcd.write((uint8_t)(serial_getch() - 97));
    }
    else if (rxbyte == 'i')
    {
      byte r1 = serial_getch();
      byte r2 = serial_getch();
      lcd.setCursor(r1 - '0', r2 - '0');
      lcd.write((char)serial_getch());
    }
    else if (rxbyte == 'k')
    {
      lcd.clear();
    }
  
  
  }
  }

Python Code:

import serial
import time
import wmi
import math

global w
w = wmi.WMI(namespace="root\OpenHardwareMonitor")


global cpu
global gpu

def updateHWData():
    global w
    global cpu
    global gpu
    hw_infos = w.Sensor()
    cpu = [] 
    gpu = [] 

    
    for sensor in hw_infos:
        #print(sensor.SensorType)
        #print(sensor.Name)
        #print(sensor.Value)
        
        if sensor.SensorType==u'Fan':
            if sensor.Name == u'Fan #1':
                cpu.insert(0,  "CPU FAN:%d RPM" % sensor.Value)
            elif sensor.Name == u'Fan #2':
                cpu.insert(1,  "MTB FAN:%d RPM" % sensor.Value)
            elif sensor.Name == u'GPU':
                gpu.insert(1,  "GPU FAN:%d RPM" % sensor.Value)
             

def serial_ports():
    ports = ['COM%s' % (i + 1) for i in range(256)]
    result = []
    for port in ports:
        try:
            s = serial.Serial(port)
            s.close()
            result.append(port)
        except (OSError, serial.SerialException):
            pass
    return result

arduino = serial.Serial(serial_ports()[1], 9600, timeout=.1000)


time.sleep(4)



arduino.write("Waiting for OpenHardwerMonitor".encode())

time.sleep(0.5)

while len(w.Sensor()) < 1:
    time.sleep(1)

cpu = [] 
gpu = []

while True:
    
    for i in range(4):
    

        updateHWData()
        arduino.write(cpu[0].encode())
        time.sleep(0.4)
        arduino.write(cpu[1].encode())
        time.sleep(1)
        arduino.write(gpu[0].encode()) 
        updateHWData()

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.