Go Down

Topic: digitalized gas flow control for robotic welders (Read 161 times) previous topic - next topic

AsaBrown

I have an Arduino MEGA2560 micro controller for a new project that I am working on. My goal is to use the arduino to output up to 20 specific voltages using a digital potentiometer(via PWM pins) to a mass flow controller for welding gas. I would like to create an easy visual display of how this will function for my boss at work so that he will have a better understanding of my project. Ive started my first arduino program and learned a ton of new things already but Ive sort of exhausted myself momentarily.. (New to writing code, but l think Im doing okay for having no education in the electronics field..)

I currently have an LED and a resister set up so that when I input a number between 0 and 10 on the serial monitor, Im looking to read the data, convert it to a number between 0 and 255, and then output it to the LED via the analogWrite function.

The problem I keep running into is that i cant seem to find the correct function to use to get my analogWrite value.. I know there are probably multiple ways to go about this issue but I'd like to use the most efficient way.

255/11 different possible user inputs would be 23.18181818.. so wouldn't I multiply the user input from the serial monitor by that 23.181818181818 to get my 0-255 analogWrite value?

My goal is to prompt the user for a 0-10 input and then to output
                 0% brightness on the LED when the number 0 is recieved from the serial port.
               10% for 1
               20% for 2
               30% for 3 all the way up to 100% from the number 10

My results have been: 1. correct percentage is printed(twice actually, with the second read out always 00%)
                                          2. No LED reaction at all
                                         
                                         
Ive tried a few different methods that Ive looked up through forums and my new favorite educator in the field, Mr. McWhorter from TopTechBoy.com, however none seem to be executing my program the way I'd like it to be executed..

I guess I should just give you the code so I learn!!!!

Code: [Select]



int greenLEDPin=2; // greenLEDpin is connected to arduino pin 2
int greenLightDuration=1000; // amount of time green LED will stay on for in milliseconds
float brightnessScaler=23.181818; // 255 divided by 11 for a 0-10 scale for user input
int endOfLoop=1000; // 1 second delay for end of a loop
int wait=250; // a quarter of a second delay


void setup() {   
      Serial.begin(9600);                                   // begin communication with serial moniter
      pinMode(greenLEDPin, OUTPUT);                         // greenLEDpin is now an output       
}

void loop() {
delay(wait); // small delay at start
     
Serial.println( "On a scale from 0 to 10, 10 being the brightest and 0 being off,");       // prompt user for data input
Serial.println( " how bright would you like the LED?. ");
 
    while(Serial.available() == 0) { } // while nothing is available from serial port do nothing
     
    while(Serial.available() == 1) { // while there is user data continue on to the next line   
             
              int userInput; // variable for user input read from whats available in serial port
              float analogValue; // local variable to store calculated data from user input

              userInput=Serial.parseInt();
              analogValue= writeValue(userInput);

                                     
                         
      Serial.print( " You selected ");
      Serial.print(userInput);
      Serial.println( "0%");
      delay(wait);
      analogWrite(greenLEDPin, analogValue);   
      delay(greenLightDuration);
      analogWrite(greenLEDPin, 0);
      delay(endOfLoop);
      }
    }
float writeValue(int userInput) {
          float writeValue;
          writeValue= brightnessScaler*userInput;
          return writeValue;
}



If anybody has a way to run my code on there own Arduino, I would appreciate ANY help/suggestions, thanks! If I left crucial information out of this post, SORRY ITS MY FIRST! I want to learn!!!

pert

1. correct percentage is printed(twice actually, with the second read out always 00%)
...and there's the problem. The "00%" is because of the line ending on the serial data. You have two options to fix it:
  • Set the line ending menu in Serial Monitor to "No line ending".
  • Modify your code so it correctly handles line endings.

AsaBrown

#2
Aug 04, 2019, 03:45 am Last Edit: Aug 04, 2019, 03:47 am by AsaBrown
...and there's the problem. The "00%" is because of the line ending on the serial data. You have two options to fix it:
  • Set the line ending menu in Serial Monitor to "No line ending".
  • Modify your code so it correctly handles line endings.

Oh wow, thanks! Mega newbie issue.... and the LED not lighting up was ALSO my fault completely(didnt double check connections!). Works perfectly now though. I appreciate the help. I will look into how to handle the line-ending through code for the future!

Go Up