Adjusting Analog Write for multiple serial inputs

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.

Pic is attached below.

With 38 posts, you should know that images of code are useless. Post your code properly, with Code Tags.

A better description of the problem than "does not accept it" would also be useful. Like how do you know that it isn't accepted?

Steve

You could find a picture of some statements like:

Serial.print("Value entered: ");
Serial.println(Voltage);

and use photoshop to merge that with your picture of code. Then, you'd KNOW whether the data was being read, or not.

The do/while statement WILL cause your program to hang forever. That seems like a really dumb thing to do.

Multiplying an int by an int produces an int.
Dividing an int by an int produces an int.

It is pointless to cast an int to an int.

The while1 statement sends the processor into an endless loop with no exit.

I doubt that anyone will write code for you, unless you are willing to pay.

Ok, gfvalvo i will remember it next time.

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 :frowning::frowning::frowning:

groundFungus i want the analogWrite function to repeat countinously so thats why i used an infinite loop.

i want the analogWrite function to repeat countinously so thats why i used an infinite loop.

Do you really think the PWM pin needs to be told over and over to do the same thing?

Well then why do we use analogWrite in a 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.

do {
analogWrite(6,dutycycle) ;
} while (1);

Maybe written like that makes it more clear?
It is stuck in that do loop forever. The do while loop reference.

 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.

line ending.jpg

line ending.jpg

Is there any way i can do select no line ending option through program code?

No, you cannot change serial monitor settings from an Arduino. You can change your code to ignore the CR and LF. See the serial input basics tutorial.

You sir, are a genius.....

So the link you gave me... dealt this issue through the use of flags and i did just that (apart from making seperate functions and it works)

Now i need some guidance on how to accept multiple values... (Need some idea about the logic of the solution).

bool flag=true ;
 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() >0 && flag==true) {
Voltage= Serial.parseInt() ;
Serial. print("The Value Entered Is ") ;
Serial.println(Voltage) ;
dutycycle=255*Voltage /5 ;
analogWrite(6,dutycycle) ;
flag=false ;
}
}

What do you mean by multiple values? An example would be helpful.

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
      }
   }
}

The code you sent works....And plus i learnt the importance of functions...

Bundle Of Thanks....