LED's not lighting up

Hello! I am quite new to coding and writing these arduino programs but Im catching on i think! I am currently stuck on an issue that ive been trying to solve for over a day now..

My goal is to have a user input data through the serial monitor to light up user's specified LED at the user's specified % of brightness for the user's prefered number of seconds.

I have no errors showing up right now but none of the LED will light up. Somehow the analogWrite function isnt doing what it's supposed to? Maybe my own defined functions at the end of the void loop ARENT functional? Ive tried a couple different options but nothing I try seems to be working.

Im attempting these baby steps to learn about arduino coding so that I may prototype a digitalized gas panel for welding where the robotic welder interfaces with the arduino to control its own weld gas flow rate.

Any advice or even just a hint would help out and alleviate some frustration!

Thanks!
an arduino NEWB

(if any information is missing please let me know so I can repost!)

int redLEDPin=4;                                // redLEDPin is connected to arduino pin 4

int yellowLEDPin=3;                             // yellowLEDPin is connected to arduino pin 3

int greenLEDPin=2;                              // greenLEDpin is connected to arduino pin 2

int endOfLoop=1000;                             // 1 second delay for end of a loop
int wait=250;                                   // a quarter of a second delay
float lightDuration;                            // amount of time in seconds that the light will stay on

float brightnessScaler=2.5247;                  // 255 divided by 101 for a 0-100 scale for user input

// void setup left out for this post***

int firstAnswer;                                // user input variable for the first answer   
int secondAnswer;                               // user input variable for the second answer
int thirdAnswer;                                // user input variable for the third answer

float PWMwriteValue;                            // PWMwriteValue initialized as variable for PWM output value 
int chosenLED;                                  // chosenLED is equal to the value of firstAnswer plus the LEDcorrection value
int LEDcorrection= 1;                           // a integer to add to users first asnswer for the correct analog pin choice

delay(wait);                                                                      // small delay at start
                  
Serial.println( " ");                                                             // blank line
Serial.print( "Which light would you like to turn on?");                          // prompt user for which light to turn on
delay(wait);                                                                      // small pause before prompting user with LED options
Serial.println(" The green(enter 1), the yellow(enter 2), or the red(enter 3)?"); // gives user options for three LED options
Serial.println( " ");                                                             // blank line

     while(Serial.available() == 0) {}                                            // do nothing while no data is recieved
     while(Serial.available() > 0) {                                              // if any data is recieved, proceed   

               firstAnswer= Serial.parseInt();                                  // variable to store user's first input from serial port
     }
Serial.println( " ");                                                             // blank line
Serial.println( "What percentage of brightness would you like the LED to");       // prompt user for input for second answer
Serial.println( " turn on to? Enter a number between 0-100. ");
Serial.println( " ");                                                             // blank line
    
      while(Serial.available() == 0) {}                                     // do nothing while no data is recieved
     
      while(Serial.available() > 0) {                                       // if any data is recieved, proceed

              secondAnswer=Serial.parseInt();                               // variable for user's second input from whats available in serial port
              PWMwriteValue= writeValueFunction(secondAnswer);              // PWMwriteValue is equal to writeValueFunction()
      }

Serial.println( " ");                                                              // blank line                                      
Serial.println( "How many seconds would you like the LED to be on for?");          // prompt user for data input
Serial.println( "Enter a number between 0-100. ");
Serial.println( " ");                                                              // blank line
    
      while(Serial.available() == 0) {}                                            // do nothing while no data is recieved
     
      while(Serial.available() > 0) {                                              // if any data is recieved, proceed
           thirdAnswer= Serial.parseInt();
      }
      
                                        // ON ONE LINE will be a print of what LED, how bright, and for how long
                                        
      delay(wait);                                               // small delay before displaying user's request
     
      
      Serial.print( "You selected the ");                        // on one line print " You selected the "
            if( firstAnswer == 1) {
              Serial.print( "green ");                           // if user selected the green LED, print 'green ' on same line
            }
            if( firstAnswer == 2) {
              Serial.print( "yellow ");                          // if user selected the yellow LED, print 'yellow ' on same line
              }
            if( firstAnswer == 3) {
              Serial.print( "red ");                             // if user selected the red LED, print 'red ' on same line
            }
          Serial.print( "LED to turn on at ");                                  // on same line, print "LED to turn on at "
          Serial.print(secondAnswer);                                           // on same line, print "the user's secondAnswer"                                               
          Serial.print( "% brightness for the duration of ");                   // on same line, print "% brightness for the duration of "
          Serial.print( thirdAnswer);                                           // on same line, print "the user's thirdAnswer"
            if( thirdAnswer == 1) {
            Serial.println( " second.");                         // if thirdAnswer is equal to 1, then print "second."
            }
            else if( thirdAnswer != 1) { 
            Serial.println( " seconds.");                        // if thirdAnswer is not equal to 1, then print "seconds."
            }

      delay(wait);                                      // small delay before LED action
      analogWrite(chosenLED, PWMwriteValue);             // user's prefered LED is turned on to the user's prefered brightness
      delay(lightDuration);                             // LED will stay on for as long as user defines
      analogWrite(chosenLED, 0);                        // LED turns back off
      delay(endOfLoop);                                 // small delay before looping back to top
     
    }

float writeValueFunction(int secondAnswer) {  // function for determining PWM output from user's 2nd input and the brightness scaler
      float writeValue;                                           // writevalue variable locally initialized for writeValueFunction
      writeValue= brightnessScaler*secondAnswer;                  // writeValue is equal to the brightnessScaler*user's second input
      return writeValue;                                          // returns writeValue variable to writeValueFunction()
      }

int correctAnalogPin(int firstAnswer) { // function for determining which LED output to activate based off of user's first answer
      
                                  // uChooseLED() = 2(for greenLED) if answerOne == 1, 
                                  // uChooseLED() = 3(for yellowLED) if answerOne == 2,
                                  // uChooseLED() = 4(for redLED) if answerOne == 3
      
      int uChooseLED;             // local variable decalred for correctAnalogPin
      uChooseLED= firstAnswer++;  // uChooseLED is equal to firstAnswer+1
      return uChooseLED;          // returns uChooseLED to uChooseLED
      }

What kind of Arduino do you have? If it is an UNO then only your yellow led is pwm capable. Only some pins can do PWM. They have a ~ by them on the board.

Delta_G:
What kind of Arduino do you have? If it is an UNO then only your yellow led is pwm capable. Only some pins can do PWM. They have a ~ by them on the board.

Its a MEGA2560. Im still stumped on this one.. im obviously overlooking something major... my functions below the void loop work but something is keeping the analogWrite function from operating. OR maybe the analogWrite function itself is messed up with the variables Ive inserted?

When you print the values you read from serial are they what you think they should be?

I also noticed that you're taking the duration between 0 and 100 and you call it seconds when you ask for it. But delay is in milliseconds. 100 milliseconds might not be enough to see the led if it isn't lit very bright. You should multiply that by 1000 to get seconds.

Delta_G:
I also noticed that you're taking the duration between 0 and 100 and you call it seconds when you ask for it. But delay is in milliseconds. 100 milliseconds might not be enough to see the led if it isn't lit very bright. You should multiply that by 1000 to get seconds.

Oh wow.. i just took a second(more like 20th) look and it looks like I never made a function to calculate a lightDuration variable based off of the third answer... it is however a bit late for me and I will make sure that that's not the case tomorrow morning! If it is.. I will be sad.. haha

Now that you've pointed that issue out, it seems to be the most obvious problem! Thanks, and I will post more if that wasnt the issue!

Delta_G:
When you print the values you read from serial are they what you think they should be?

So i fixed the light duration issue but it doesn't seem to solve my problem of no LED action at all.. Does anything else stick out to you indicating why the analogWrite function isn't lighting the LEDs? They all work on a different program that doesn't use variables for the analogWrite function so I think may not be passing information correctly to it?

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:

analogWrite() does not expect a float as a parameter. It expects an integer between 0 and 255. Why not just use a simple map() to convert 0-100 to 0-255?

Steve

analogWrite only works on 3,5,6,9,10,11 on an Uno (or any '328P based board):

int redLEDPin=4;                                // redLEDPin is connected to arduino pin 4
int yellowLEDPin=3;                             // yellowLEDPin is connected to arduino pin 3
int greenLEDPin=2;                              // greenLEDpin is connected to arduino pin 2

And these only need to be byte, not int.

CrossRoads:
analogWrite only works on 3,5,6,9,10,11 on an Uno (or any '328P based board):

int redLEDPin=4;                                // redLEDPin is connected to arduino pin 4

int yellowLEDPin=3;                            // yellowLEDPin is connected to arduino pin 3
int greenLEDPin=2;                              // greenLEDpin is connected to arduino pin 2






And these only need to be byte, not int.

I have the arduino mega. Why do they need to be bytes?

“Why do they need to be bytes?”

They don’t need to be ‘byte’, they can be ‘byte’.

larryd:
Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:
Simple guide for inserting images in a post - Website and Forum - Arduino Forum

Here's picture of my setup-

I'll have to look up how to write a schematic when I have more time, sorry. You should have a pretty good view of how the LEDs are wired up on the breadboard though.. What you cant really see is that the green wire goes to pin2, yellow wire to pin3, and red wire to pin4.

I tested the map() suggestion and I liked it much more than how I was attempting it before. However.. none of the LEDs light up still.. I get the correct print out in the serial monitor though, so I still must be overlooking something major..

(I hope the link is viewable! Let me know what else I can provide!)

Does the simple Blink.ino sketch work on these pins as they are wired?

larryd:
Does the simple Blink.ino sketch work on these pins as they are wired?

use the Blink.ino sketch as in to check to see the LEDs are set up correctly, right?

Does this blink the LED on pin 2 ?

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin =  2;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
int redLEDPin=4;                                // redLEDPin is connected to arduino pin 4

int yellowLEDPin=3;                             // yellowLEDPin is connected to arduino pin 3

int greenLEDPin=2;                              // greenLEDpin is connected to arduino pin 2

int endOfLoop=1000;                             // 1 second delay for end of a loop
int wait=250;                                   // a quarter of a second delay
float lightDuration;                            // amount of time in seconds that the light will stay on

float brightnessScaler=2.5247;                  // 255 divided by 101 for a 0-100 scale for user input

// void setup left out for this post***

int firstAnswer;                                // user input variable for the first answer   
int secondAnswer;                               // user input variable for the second answer
int thirdAnswer;                                // user input variable for the third answer

float PWMwriteValue;                            // PWMwriteValue initialized as variable for PWM output value 
int chosenLED;                                  // chosenLED is equal to the value of firstAnswer plus the LEDcorrection value
int LEDcorrection= 1;                           // a integer to add to users first asnswer for the correct analog pin choice

delay(wait);                                                                      // small delay at start
                  
Serial.println( " ");                                                             // blank line
Serial.print( "Which light would you like to turn on?");                          // prompt user for which light to turn on
delay(wait);                                                                      // small pause before prompting user with LED options
Serial.println(" The green(enter 1), the yellow(enter 2), or the red(enter 3)?"); // gives user options for three LED options
Serial.println( " ");                                                             // blank line

     while(Serial.available() == 0) {}                                            // do nothing while no data is recieved
     while(Serial.available() > 0) {                                              // if any data is recieved, proceed   

               firstAnswer= Serial.parseInt();                                  // variable to store user's first input from serial port
     }
Serial.println( " ");                                                             // blank line
Serial.println( "What percentage of brightness would you like the LED to");       // prompt user for input for second answer
Serial.println( " turn on to? Enter a number between 0-100. ");
Serial.println( " ");                                                             // blank line
    
      while(Serial.available() == 0) {}                                     // do nothing while no data is recieved
     
      while(Serial.available() > 0) {                                       // if any data is recieved, proceed

              secondAnswer=Serial.parseInt();                               // variable for user's second input from whats available in serial port
              PWMwriteValue= writeValueFunction(secondAnswer);              // PWMwriteValue is equal to writeValueFunction()
      }

Serial.println( " ");                                                              // blank line                                      
Serial.println( "How many seconds would you like the LED to be on for?");          // prompt user for data input
Serial.println( "Enter a number between 0-100. ");
Serial.println( " ");                                                              // blank line
    
      while(Serial.available() == 0) {}                                            // do nothing while no data is recieved
     
      while(Serial.available() > 0) {                                              // if any data is recieved, proceed
           thirdAnswer= Serial.parseInt();
      }
      
                                        // ON ONE LINE will be a print of what LED, how bright, and for how long
                                        
      delay(wait);                                               // small delay before displaying user's request
     
      
      Serial.print( "You selected the ");                        // on one line print " You selected the "
            if( firstAnswer == 1) {
              Serial.print( "green ");                           // if user selected the green LED, print 'green ' on same line
            }
            if( firstAnswer == 2) {
              Serial.print( "yellow ");                          // if user selected the yellow LED, print 'yellow ' on same line
              }
            if( firstAnswer == 3) {
              Serial.print( "red ");                             // if user selected the red LED, print 'red ' on same line
            }
          Serial.print( "LED to turn on at ");                                  // on same line, print "LED to turn on at "
          Serial.print(secondAnswer);                                           // on same line, print "the user's secondAnswer"                                               
          Serial.print( "% brightness for the duration of ");                   // on same line, print "% brightness for the duration of "
          Serial.print( thirdAnswer);                                           // on same line, print "the user's thirdAnswer"
            if( thirdAnswer == 1) {
            Serial.println( " second.");                         // if thirdAnswer is equal to 1, then print "second."
            }
            else if( thirdAnswer != 1) { 
            Serial.println( " seconds.");                        // if thirdAnswer is not equal to 1, then print "seconds."
            }

      delay(wait);                                      // small delay before LED action
      analogWrite(chosenLED, PWMwriteValue);             // user's prefered LED is turned on to the user's prefered brightness
      delay(lightDuration);                             // LED will stay on for as long as user defines
      analogWrite(chosenLED, 0);                        // LED turns back off
      delay(endOfLoop);                                 // small delay before looping back to top
     
    }

float writeValueFunction(int secondAnswer) {  // function for determining PWM output from user's 2nd input and the brightness scaler
      float writeValue;                                           // writevalue variable locally initialized for writeValueFunction
      writeValue= brightnessScaler*secondAnswer;                  // writeValue is equal to the brightnessScaler*user's second input
      return writeValue;                                          // returns writeValue variable to writeValueFunction()
      }

int correctAnalogPin(int firstAnswer) { // function for determining which LED output to activate based off of user's first answer
      
                                  // uChooseLED() = 2(for greenLED) if answerOne == 1, 
                                  // uChooseLED() = 3(for yellowLED) if answerOne == 2,
                                  // uChooseLED() = 4(for redLED) if answerOne == 3
      
      int uChooseLED;             // local variable decalred for correctAnalogPin
      uChooseLED= firstAnswer++;  // uChooseLED is equal to firstAnswer+1
      return uChooseLED;          // returns uChooseLED to uChooseLED
      }

I don't see where correctAnalogPin() is ever called.

I don't see where chosenLED is ever set except for default zero where it is defined.

I don't see the whole sketch so I can't say what is missing.

GoForSmoke:
I don't see where correctAnalogPin() is ever called.

I don't see where chosenLED is ever set except for default zero where it is defined.

I don't see the whole sketch so I can't say what is missing.

Oh man, I swear Im working on my coding practices! Still much to learn/remember! I apreciate the reply!!! Im working right now, but I have my laptop so I was able to correct the code(calling the correctAnalogPin()---- chosenLED= correctAnalogPin(firstAnswer)). I will test it out in a little while when Im on break! IM SO EXCITED!!! That HAS to be the issue..

Success! It works now! After I made the correction to the correctAnalogPin(), the LEDs started to light up! BUT wrong ones were being lit(entered1 for green&got nothing, enter2 for yellow&got greenLED, entered3 for red%got yellowLED, and i entered4 assuming it would light up the redLED based off of what was happening and BINGO… it lit the redLED).

I looked at the function one more time and instead of having it read:

int correctAnalogPin(int firstAnswer) {
int uChooseLED;
uChooseLED= firstAnswer++; // <--------------
return uChooseLED;

I changed it to:

int correctAnalogPin(int firstAnswer) {
int plusOne=1; //<------------
int uChooseLED;
uChooseLED= firstAnswer+ plusOne; //<---------
return uChooseLED;

Thanks for all of your help everybody!! You are awesome and I hope I’ll be as helpful as you soon!
[/quote]