Hello
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_IN,INPUT);
pinMode(PWM_Pin_OUT, OUTPUT);
Serial.begin(9600);
PWMVal=0;
}
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;
delay(100);
if (PWM_Val_OUT <30);
{
PWMVal= 0;
delay(1000);
}
if (PWM_Val_OUT >=30 && PWM_Val_OUT <50);
//if (PWM_Val_OUT >= 30);
{
PWMVal= 20;
delay(100);
}
if (PWM_Val_OUT >=50 && PWM_Val_OUT <=100);
//if (PWM_Val_OUT >= 50);
{
PWMVal= 198;
delay(100);
}
analogWrite(PWM_Pin_OUT, PWMVal);
//Serial.print(PWMVal, DEC);
Serial.print(PWMVal, DEC);
Serial.print("PWMvaL");
Serial.print(PWM_Val_OUT, DEC);
Serial.print("% inputinp");
Serial.print("\n");
delay(10);
/*Serial.print(PWM_Val_IN, DEC);
Serial.print("% input");
Serial.print(analogduty, DEC);
Serial.print("%");
Serial.print(analogontime, DEC);
Serial.print("analogontime");
Serial.print(analogofftime, DEC);
Serial.print("analogofftime");
Serial.print(analogfreq, DEC);
Serial.print("freq");
Serial.print(PWM_Pin_OUT, DEC);
Serial.print("%%");
Serial.print("\n");
*/
}
type or paste code here