LED_update is working on one code but not the other

the first code is one i got off the internet and the LED_update works but the second which I've modded doesn't and i don't know why

any help would be grate

int ledpinr = 9;
int ledping = 10;
int ledpinb = 11;
int red = 255;
int blue = 255;
int green = 255;
int val;
boolean busy = false;
const int pingPin = 7;

void setup() {
  Serial.begin(9600);
  led_update();
} 

void led_update()
{
  //Set PWM on pins 9 10 and 11
  analogWrite(ledpinr, red);
  analogWrite(ledping, green);
  analogWrite(ledpinb, blue);
  //delay(500);
}

second code

int ledpinr = 5;
int ledping = 6;
int ledpinb = 7;
int trigPin1 = 8;
int echoPin1 = 9;
int red = 255;
int blue = 255;
int green = 255;
int val;
boolean busy = false;
long duration1, distance1;



void setup() {
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  Serial.begin(9600);
  led_update();

}
void loop(){

void led_update()
{
  //Set PWM on pins 9 10 and 11
  analogWrite(ledpinr, red);
  analogWrite(ledping, green);
  analogWrite(ledpinb, blue);
  //delay(500);
}

In your second code, you try to define led_update() inside of loop(). That is NOT allowed.

Even if it were, the loop() function is missing a } at the end.

Hi,
What do you mean by "doesn't work"?

Doesn't compile?
Doesn't upload?
Doesn't display like it should?

What is it supposed to do?
Sorry like many here our crystal ball is out for grease and oil change.. :o

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
What do you mean by "doesn't work"?

Doesn't compile?
Doesn't upload?
Doesn't display like it should?

What is it supposed to do?
Sorry like many here our crystal ball is out for grease and oil change.. :o

Thanks.. Tom... :slight_smile:

it keeps coming up with this error 'led_update' was not declared in this scope

"void loop(){" is missing the closing "}"! Change it to"void loop(){}".

it keeps coming up with this error ‘led_update’ was not declared in this scope

And the compiler will continue to say that until you end loop() BEFORE the implementation of the led_update() function.

this is not the hole code so there is a } and the end

Hi,

void loop(){

void led_update()
{
  //Set PWM on pins 9 10 and 11
  analogWrite(ledpinr, red);
  analogWrite(ledping, green);
  analogWrite(ledpinb, blue);
  //delay(500);
}

try;

void loop(){

led_update();

//rest of code
}

void led_update()
{
  //Set PWM on pins 9 10 and 11
  analogWrite(ledpinr, red);
  analogWrite(ledping, green);
  analogWrite(ledpinb, blue);
  //delay(500);
}

Tom… :slight_smile:

here the rest of the code I’m not sure if u want me to put the rest of the code above analogWrite

this still a work in progress so don’t Judge Me too hard

int ledpinr = 5;
int ledping = 6;
int ledpinb = 7;
int trigPin1 = 8;
int echoPin1 = 9;
int red = 255;
int blue = 255;
int green = 255;
int val;
boolean busy = false;
long duration1, distance1;



void setup() {
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  Serial.begin(9600);
  led_update();

}
void loop(){

void led_update()
{
  //Set PWM on pins 9 10 and 11
  analogWrite(ledpinr, red);
  analogWrite(ledping, green);
  analogWrite(ledpinb, blue);
  //delay(500);
}
void color_morph(int* value, int maxval, int get_brighter)
{
  if (maxval > 255) {
    maxval = 255;
  }
  else if (maxval < 0) {
    maxval = 0;
  }
  for (int i = 0; i < maxval; i++)
  {
    if (get_brighter) {
      if (*value > 0) {
        (*value)--;
      }
    }
    else {
      if (*value < 255) {
        (*value)++;
      }
    }
  }
    led_update();
    delay(5);
      digitalWrite(trigPin1, LOW);
        
      delayMicroseconds(2);

      digitalWrite(trigPin1, HIGH);

      delayMicroseconds(10);

      digitalWrite(trigPin1, LOW);

      duration1 = pulseIn(echoPin1, HIGH);

      distance1 = (duration1 / 2) / 29.1;

      Serial.print(distance1);
      Serial.println("cm");
    }
  }
}
void loop(){

void led_update()
{

You can NOT declare a function inside of another function.

Move the ENTIRE led_update() function AFTER the end of loop().

PaulS:

void loop(){

void led_update()
{



You can NOT declare a function inside of another function.

Move the ENTIRE led_update() function AFTER the end of loop().

what do u mean it in the setup it get declared before the loop() it not declared in the loop and if i put it after the loop it dose do any thing and just come up with this error: a function-definition is not allowed here before '{' token

Hi,
Read post #7.

Tom.. :slight_smile:

In the IDE, use tools -> autoformat; after that (parts of) your code will look like

void loop() {

  void led_update()
  {

Because void led_update() does not start at the beginning of a line, it indicates that the function before it is not closed. So you're defining a function inside another function (as was indicated a few times); that is not allowed.

The end of your code (after the autoformat) is

}
}

You should not have two } starting at the same position (beginning of the line).

Move the last } to after the void loop(); after that, do an autoformat again.

void loop() {
}

void led_update()
{
  ...
  ...
}

void color_morph(int* value, int maxval, int get_brighter)
{
  ...
  ...
}
}

You still have two } at the beginning of consecutive lines. The second one is redundant and will still cause a compiler error; remove it.

Note:
95% of the time or more, problems are in the code that one does not show; always show complete code (or attach it if it exceeds the 9000 character limit).

thank for the help that one problem down just a gatherer thousand to go