How to make PWM signal with Hardware Serial ? Please Help

Greetings everyone, My project is about Making PWM signal by using hardware serial(from 2 Mega 2560 Arduino connect by using each TX,RX).The sent value varied by user coding but the value for receiving board receive from sender.Then the value will become the delay of high state of PWM signal (Duty cycle).The problem are when I sent the value it happened the right value on lcd screen(16x2 i2c lcd) But it wouldn’t happen the correct PWM waveform.The waveform is unpredictable even the sender still send the value such as I code “a” as 80
this mean the 10kHZ pulse must have width of pulse 80 microseconds but shown duty cycle (measured by Osilloscope ) is 1.2%
Coding
Sender


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

void loop()
{
Serial.write(80);
}


Receiver

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x3F,20,4);
int a;

void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(8, OUTPUT);
lcd.init();
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Waiting for DATA”);
}

void loop() {
// see if there’s incoming serial data:
int i=0;
if((Serial.available() > 0)&& (i==0))
{
i++;
a = Serial.read();
}
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(a);
PWM(a);
}

void PWM(int level)
{
int DlayH,DlayL;
DlayH=level;
DlayL=100-DlayH;
if(DlayH==0)
{
digitalWrite(8,LOW);
}
else if(DlayH==100)
{
digitalWrite(8,HIGH);
}
else
{
digitalWrite(8,HIGH);
delayMicroseconds(DlayH);
digitalWrite(8,LOW);
delayMicroseconds(DlayL);
}
}


(2 arduinos are connected only TX0,RX0,GND together and LCD connect with SCL SDL for receiving board)
Thank you for HELP.

P.S I’m sorry for my english.

Before posting your code, please do an autoformat (your indentation is quite thorougly messed up) and post within code tags, as clearly described in the forum sticky.

Pingponge:
Greetings everyone, My project is about Making PWM signal by using hardware serial

WHY ?

Why not send the PWM value to the receiver and let the receiver generate the PWM signal ?

That way you only need to send a message when the value changes.

…R

OK, not on mobile now so better view of your code. Many issues.

  1. don’t use pin numbers in your code - give your pins a name (variable), and initialise that. Makes your code more readable, and if you change the pin later you can just change that one line.

  2. your loop.

void loop() {
  // see if there's incoming serial data:
   int i=0;
    if((Serial.available() > 0)&& (i==0))
    {
    i++;
    a = Serial.read();
    }
                          lcd.clear();
                          lcd.setCursor(0, 1);
                          lcd.print(a); 
                          PWM(a);
  }

a) This variable i doesn’t do anything useful.
b) You update your display every time loop() runs. No need for that; only takes time. That brings us to:

  1. the PWM function. You say you want a 10 kHz signal - you may be able to get 3-5 kHz, don’t expect more with this code. Time spent in both loop() and in this code makes that idea fail.
void PWM(int level)
{
  int DlayH,DlayL;
  DlayH=level;
  DlayL=100-DlayH;
      if(DlayH==0)
  {
          digitalWrite(8,LOW);
  }
      else if(DlayH==100)
  {
          digitalWrite(8,HIGH);
  }
      else
  {
          digitalWrite(8,HIGH);
          delayMicroseconds(DlayH);
          digitalWrite(8,LOW);
          delayMicroseconds(DlayL);
  }
}

a) The if block and calculations take time. The digitalWrite takes time. No chance of making your 10 kHz this way.
b) delayMicroseconds has a minimum of 3 us according to the documents - not sure about the resolution, but it may be the 4 us steps micros() uses as well. All in all not a very accurate function.

To produce your 10 kHz PWM with variable duty cycle use timer interrupts, or the built-in PWM functionality (read the data sheet and example code for which registers to manipulate - a simple analogWrite() won’t do).

Robin2:
Why not send the PWM value to the receiver and let the receiver generate the PWM signal ?

Explanation is terrible indeed, but the posted code is doing just that.