Read PWM duty from a pin and write PWM to a pin


I've been messing with a Arduino nano to get analogWrite(); command right to work. Arduino nano reads PWM input on a pin 3. Then through the forumulas it calculates the input duty cycle. Then with the duty cycle I want to control the output pin PWM duty cycle. If the input duty cycle is below then the output PWM pin will be PWMval=0 and if it is over a certain value it is x and if it is over another duty cycle value then the output duty cycle will be y and vice versa. I have a problem with my code, that it reads nicely the INPUT duty cycle, but it analogWrites the output pin to a PWM value ~200 and it is not changing when the input is changing. The another arduino is sending PWM duty of 39% 10s and then another 10s 59%. to arduino nano input PWM pin.
I do not understand why arduino nano does not want to update the PWMVal when in the code I have said to do that. With the output PWM pin I'm using the npn transistor to control a LED array on a car.

I will post a code below. Any help is appreciated. I also apologize that the code is like that, I know there is a lot of trash which maybe is not needed etc.

#include <Servo.h>

//#define pulse_ip 7
int PWM_Pin_IN = 3;
int PWM_Pin_OUT = 9;
int PWM_Val_IN;
int PWM_Val_OUT;
int PWMVal=0;
//int setPwmFrequency(8, 1024);
// int analogontime,analogofftime,analogduty;
unsigned int analogontime, analogofftime;
float analogfreq,analogduty,analogperiod;
void setup()
  pinMode(PWM_Pin_OUT, OUTPUT);
void loop()
  PWM_Val_IN = analogRead(PWM_Pin_IN);

   analogontime = pulseIn(PWM_Pin_IN,HIGH);
   analogofftime = pulseIn(PWM_Pin_IN,LOW);
   analogperiod = analogontime+analogofftime;
   analogfreq = 1000000.0/analogperiod;
   analogduty = (analogontime/analogperiod)*100;
   //analogduty=  PWM_Val_OUT;
    PWM_Val_OUT = analogduty;

if (PWM_Val_OUT <30);
  PWMVal= 0;
if (PWM_Val_OUT >=30 && PWM_Val_OUT <50);
//if (PWM_Val_OUT >= 30);
PWMVal= 20;
if (PWM_Val_OUT >=50 &&  PWM_Val_OUT <=100);
//if (PWM_Val_OUT >= 50);

  PWMVal= 198;
 analogWrite(PWM_Pin_OUT, PWMVal);
 //Serial.print(PWMVal, DEC);
 Serial.print(PWMVal, DEC);
Serial.print(PWM_Val_OUT, DEC);
Serial.print("% inputinp");

/*Serial.print(PWM_Val_IN, DEC);
Serial.print("% input");
Serial.print(analogduty, DEC);
Serial.print(analogontime, DEC);
Serial.print(analogofftime, DEC);
Serial.print(analogfreq, DEC);
Serial.print(PWM_Pin_OUT, DEC);


type or paste code here

if (PWM_Val_OUT >=50 &&  PWM_Val_OUT <=100); <<===

PWMVal = 198;
 analogWrite(PWM_Pin_OUT, PWMVal);

Could be something in here ?

Yeah. It takes that value of 198 which I can see in serial monitor as well. But what is wrong in there?
The semicolon should not be there or what?

The semicolon terminates all your if statements…. lose all of them !

Thank you for the help! I will give it a try and let you know if it works!

int PWM_Pin_IN = 3;

Is that pin 3 or pin A3 ?
Which pin is the PWM input signal connected to ?

A PWM signal has only 2 voltage levels so using analogRead() is probably not doing what you want

It reads analog Duty cycle as I can see on a serial monitor. Problem is with output duty cycle generation. But I think I have found solution for the problem.

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