Hey there fellow makers, well the level which the people on this forum have reached, maker is a relaltively small term.
Anyways, i have this code i made for my low pass RC filter.. Which converts PWM to stable DC waves...
The problem with the code is that whenever i type the value in serial monitor it does not accept it and the following code after serial.print doesnt work...
Further more i would really appreciate it if you would devise a code with which i can analog write multiple voltages... In form of a loop condition or whatever you prefer best.
Slipstick, so what happens is that the the tx pin on the arduino blinks when i send the value but the pwm does not happen... But whenever i use simple analogWrite in the loop it works... So this means problem is with the coding..... One more thing i am checking the output of my program through varying the brightness of an led. And in the faulty code written, the led does not light up.
PaulS, you are right, never thought of that, my bad
groundFungus i want the analogWrite function to repeat countinously so thats why i used an infinite loop.
---------------Post Edited---------------
int Voltage ;
int dutycycle;
void setup() {
Serial.begin(9600);
TCCR0B = TCCR0B & B11111000 | B00000001 ;
//Gives Pwm frequency of 62500 on pin 6
pinMode(6,OUTPUT) ;
}
void loop() {
Serial.println("Enter Voltage From 0 To 5 Volts") ;
if (Serial.available() > 0) {
Voltage= Serial.parseInt() ;
Serial. println("The Value Entered Is ") ;
Serial.print(Voltage) ;
} dutycycle=255*Voltage /5 ;
do {
analogWrite(6,dutycycle) ;
}
while (1) ;
}
I just did what PaulS told me and the line "The Value Entered Is" is not displaying on the serial monitor when i give an input.
int Voltage ;
int dutycycle;
void setup() {
Serial.begin(9600);
TCCR0B = TCCR0B & B11111000 | B00000001 ;
//Gives Pwm frequency of 62500 on pin 6
pinMode(6,OUTPUT) ;
Serial.println("Enter Voltage From 0 To 5 Volts") ;
}
void loop() {
if (Serial.available()) {
Voltage= Serial.parseInt() ;
Serial. print("The Value Entered Is ") ;
Serial.println(Voltage) ;
dutycycle=255*Voltage /5 ;
analogWrite(6,dutycycle) ;
}
}
I know what a do while loop does... I am asking why do we use analog write in a void loop... Its because we want it to perform repetitively.
Anyways just changed my code, it is working better but analogWrite only happens for a split second and the code re-runs and a line comes on serial monitor" The Value Entered Is 0"
Set the line endings in serial monitor to "no line ending". The program is getting your number and a linefeed and/or carriage return and interpreting the LF or CR as a 0. It sets the duty cycle to your number then, next time through loop(), sets the duty cycle to 0.
Meaning i want to change the voltage.... How would i edit the program....
Let me elaborate, the current program only let me input voltage only once and then fixes it.. ... Now suppose i want to change the voltage and i want to do this however many times i want...
Using a lesson from serial input basics, this is how I would do it. The code receives one character. That is all you need to input an int from 0 to 5. It checks if the received character is a number from 0 to 5 and ignores any character that is not (including CR and LF). Then it changes the char data type (receivedChar) to an int data type (Voltage).
I always use functions. The planning and implementing a program tutorial explains the how and why of programming using functions. It is a very important concept in programming.
int Voltage ;
int dutycycle;
char receivedChar;
boolean newData = false;
void setup()
{
Serial.begin(9600);
TCCR0B = TCCR0B & B11111000 | B00000001 ;
//Gives Pwm frequency of 62500 on pin 6
pinMode(6, OUTPUT) ;
Serial.println("Enter Voltage From 0 To 5 Volts") ;
}
void loop()
{
recvOneChar();
if (newData)
{
Serial. print("The Value Entered Is ") ;
Serial.println(Voltage) ;
dutycycle = 255 * Voltage / 5 ;
analogWrite(6, dutycycle) ;
newData = false; // cleat flag
}
}
void recvOneChar()
{
if (Serial.available() > 0)
{
receivedChar = Serial.read();
//is receivedChar a number from 0 to 5?
if (receivedChar >= '0' && receivedChar <= '5')
{
Voltage = receivedChar - '0'; // change char to int
newData = true; // set flag
}
}
}