What am i doing wrong with this code?

int ledPin = 9; // LED is connected to pin 12
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
int value = 0;


void setup()
{
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(switchPin, INPUT); // Set the switch pin as input
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
if (val == LOW) // check if the button is pressed
digitalWrite(ledPin, HIGH); // turn LED on
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
} else {
void loop(){
for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
analogWrite(ledPin, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
analogWrite(ledPin, value);
delay(30);
}
this is a button code i tried to write so when the button is not pressed it will fade, but i keep getting this error
error: expected unqualified-id before 'else'
can someone please explain what i am doin wrong or posting a code of they're own? I am a complete beginner at this coding stuff.
:3 alc

missing a curly

if (val == LOW) // check if the button is pressed
digitalWrite(ledPin, HIGH); // turn LED on

Should be:

if (val == LOW) // check if the button is pressed
{
digitalWrite(ledPin, HIGH); // turn LED on

 

this is the error i get if i

this is the error i get if i use that

In function ‘void loop()’:

error: a function-definition is not allowed here before ‘{’ toke

maybe some more curlies?

I don’t actually speak C. 8-(

But I know to make my parentheses symmetric. Always. Smileys are the only exception.

Check for all { } and ( ) and [ ] . Are they properly opened/closed?

Also: I am not sure if you need that second and third void loop() declaration. But then again: my language is Perl, not C.

test this

I am a computer science student at Omdurman Ahliya university in sudan <o:p></o:p>I am not a professional in c language but I will try to solve your problem , <o:p></o:p>first Error you have a two functions with same name ‘loop’<o:p></o:p>

the ‘else’ error may be solved as below

if you have a ‘pinMode’ function , ‘analogWrite’ function and ‘digitalWrite’ function this code below will be run:
int ledPin = 9; // LED is connected to pin 12
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
int value = 0;

void setup()
{
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(switchPin, INPUT); // Set the switch pin as input
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
if (val == LOW)
// check if the button is pressed
digitalWrite(ledPin, HIGH); // turn LED on
delay(100)
else {
digitalWrite(ledPin, LOW);
delay(100);
}
for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
analogWrite(ledPin, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
analogWrite(ledPin, value);
delay(30);
}
}

A little tip. Anytime you

A little tip. Anytime you write code and you find yourself using a bracket to group code { and } ALWAYS put the close bracket in the code right away.

If blah blah blah
{
}

And then write the code inside the IF statement. This prevents some of the problems above where you close out a bracket and it applies to the function instead of somewhere else. Also put brackets on their own line so it makes it readable

IF blah blah blah {
code code code
}

Isn’t as readable.