Serial monitor not working , in second program

Hi
Here are two programs , in the first one serial monitor is printing some numbers = Ok but on second one serial monitor is blank , how to fix it ?

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-serial-plotter
 */

void setup() {
  Serial.begin(115200); 
}

void loop() {
  int y1 = analogRead(PA0);

  Serial.println(y1);

  delay(100);
}
#include <EEPROM.h> 

unsigned long amp = 0;

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306_STM32.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

void setup() {
  Serial.begin(115200);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  display.display();
  display.clearDisplay(); 
  display.drawPixel(100, 15, WHITE);

  pinMode(PA8, PWM);
  pinMode(PA9, PWM);
  pinMode(PB8, INPUT_PULLUP);
  pinMode(PB9, INPUT_PULLUP);
  TIMER1_BASE->CCER = (1 << 4) | (1 << 0);
  TIMER1_BASE->CCMR1 = (1 << 13) | (1 << 12) | (1 << 5) | (1 << 4);
  TIMER1_BASE->PSC = 0;
  TIMER1_BASE->CCR1 = 0 ;
  TIMER1_BASE->CR1 = 1;
  EEPROM.read(500, (uint16*)&amp);

}

void loop() {
  if (digitalRead(PB8) == 0) {
    amp++;

    EEPROM.write(500, amp);
  }
  if (digitalRead(PB9) == 0) {
    amp--;

    EEPROM.write(500, amp);
  }

  TIMER1_BASE->ARR = amp * 2;
  TIMER1_BASE->CCR2 = amp ;
  Serial.print(" Ubal =  ");
  Serial.println(amp);
  
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);

  display.setCursor(64, 28);
 // display.print(36000000 / amp / 2);
   display.print(amp);
  display.display();

  delay(20);
}

What microcontroller are you using?

What ever it is are you sure that PA8, PA9, PB8, or PB9 are not used by Serial?

Are you aware of the fact that an EEPROM can only do 100.000 write-cycles?

If your code happened to worked "the wrong way" which means the if-condition

if (digitalRead(PB8) == 0)

was true all the time your 100.000 write-cycles ran down in
100.000 / (1/0.02 seconds) = 2000 seconds which are 33 minutes?

best regards Stefan

I am using a bootloader , stm32f103

yes,but my problem is not with EEPROM

vrey important information especcially if you use something different than an ATmega328P

I never used a stm32f103
If you look onto the pinout


PA9 is mentioned as TX1

It might be that you have exactly chosen that IO-pin that is used for serial communication.
But as I don't know the stm32f103 at all this might not be true.
What happends if you comment out the lines that are related to PA9?

For narrowing down the problem I would comment out all code that does something with PA8, PA9, PB8, PB9 and then put one line of code back into the code and test again

best regards Stefan

image

Try using a different pin, or different Serial.

There are 3 OLED lines that cause the problem, I marked them by ++++++++++++++++++++
So it is looks that nothing can be done ???

#include <EEPROM.h>

unsigned long amp = 0;

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306_STM32.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

void setup() {
  Serial.begin(115200);

// display.begin(SSD1306_SWITCHCAPVCC, 0x3C);//+++++++++++++++++++++++++++++
 //display.display(); //++++++++++++++++++++++++++++++++++++++++++++++++++++

  display.clearDisplay();
  display.drawPixel(100, 15, WHITE);

  pinMode(PA8, PWM);
  pinMode(PA9, PWM);
  pinMode(PB8, INPUT_PULLUP);
  pinMode(PB9, INPUT_PULLUP);
  TIMER1_BASE->CCER = (1 << 4) | (1 << 0);
  TIMER1_BASE->CCMR1 = (1 << 13) | (1 << 12) | (1 << 5) | (1 << 4);
  TIMER1_BASE->PSC = 0;
  TIMER1_BASE->CCR1 = 0 ;
  TIMER1_BASE->CR1 = 1;
  EEPROM.read(500, (uint16*)&amp);
}

void loop() {
  int y1 = analogRead(PA0);

  if (digitalRead(PB8) == 0) {
    amp++;

    EEPROM.write(500, amp);
  }
  if (digitalRead(PB9) == 0) {
    amp--;

    EEPROM.write(500, amp);
  }

  TIMER1_BASE->ARR = amp * 2;
  TIMER1_BASE->CCR2 = amp ;

  Serial.print("  y1  = ");
  Serial.println(y1);
  Serial.print(" Ubal =  ");
  Serial.print(amp);

  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(64, 28);
  display.print(amp);
  // display.display(); +++++++++++++++++++++++++++++++++++++++++++++

}

Use different pins so you don't conflict with the display or the Serial port.

OLED lines does not reflect to your Serial, the important thung are pins PA8 PA9 and your manupulation with TIMER1

I am trying to use different serial, but it is not working



void setup() {
  Serial.begin(115200);
  Serial2.begin(115200); 

}

void loop() {
  int y1 = analogRead(PA0);
// Serial.println(y1);
  Serial2.println(y1);
    // display.display(); +++++++++++++++++++++++++++++++++++++++++++++

  delay(100);
}

I presume only 1 Serial will be connected to the USB that is connected to your PC.

Why don't you just change the pins you're using?

I removed timer, just OLED is causing the problem, probably serial monitor and OLED are using the same I2C, so I need to remap one of them, any suggestion how to do it = remapping OLED to I2C2, pins PB6,7 change to PB10,11 ?

#include <EEPROM.h>

unsigned long amp = 0;

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306_STM32.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

void setup() {
  Serial.begin(115200); 
// display.begin(SSD1306_SWITCHCAPVCC, 0x3C);//+++++++++++++++++++++++++++++
//display.display(); //++++++++++++++++++++++++++++++++++++++++++++++++++++

  display.clearDisplay();
  display.drawPixel(100, 15, WHITE);
}

void loop() {
  int y1 = analogRead(PA0);

  Serial.println(y1);
    // display.display(); +++++++++++++++++++++++++++++++++++++++++++++

  delay(100);
}

no, the Serial has nothing to do with i2c

on an stm32 MCU? .. are you sure about that?

serial does not use I2C

or as a first step don't use PA8 and PA9 at all just for a test if
not using PA8 / PA9 makes the serial monitor receive characters again

remap PA8 and PA9 to different IO-pins

which one fits best depends on what you are additionally using
You should provide full information which hardware is connected to what IO-pin

now not as much as before :slight_smile:
But... by default Serial1 is situated on PA9 PA10, PB6 PB7 pins are only alternative and does not used in Arduino toolchain, as i know

Serial send/receive is done with a completely different pattern of how IO-pins are switched HIGH-LOW than I2C-IO-pins are switched HIGH-LOW.

A lot of microcontrollers offer to use hardware-I2C or hardware-serial on multiples IO-pins.

The micorcontroller can configured to use IO-Pin named PB7
to be a SDA-line for I2C
or
to act as T4C2
or
to act as RX1

of course there can be conflicts if different libaries want to use the same IO-pin.

@b707
if you want help about this you should provide all code including all the code from the libraries you are using.
If you don't know how to provide this
how about

best regards Stefan

Post #13 = I am not using PB8, PB9 only PB6 and PB7.

except they can use the same pins... so I guess that's where the patterns overlap.

Pretty sure he already dd that in post #13. You should pay more attention to the detail.