Let's Make Robots!

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 
 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

code below then works so:-

with pin 2 LOW the LED will Blink on and off

with pin 2 HIGH it will fade the LED on and off

(by the way is the LED really connected to pin 12 as in the comment !!)

 

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 {  
     fade();
     }
     }
    
void fade(){
    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 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.

I am a computer science student at Omdurman Ahliya   university in sudan

I am  not a professional in c language but I will try to solve your problem ,  

first Error you have a two functions with same name 'loop'

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

Indeed, arduino only uses one "void loop()". Multiple ones will confuse it.  Rik is right, there has to be a brace after an if() statement, and an end brace after the block of code being iffed.

The Arduino IDE will highlight matched parenthises, brackets, and braces. Click next to a parenthesis and it'll draw a block around it's "mate". If that block isn't where you think it is, you either have some missing or have extra.

I'd highly suggest taking a good look at the examples in the sketchbook. They'll show you the "layout" of things pretty well.

To fix it, take out the second "loop(){" line entirely, add a } after the if(blah), then add two more } at the very end (on two seperate lines). Your for()'s are correct, but your if() never starts or ends (due to missing braces), and your loop() never ends (missing brace).

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 use that 

 

In function 'void loop()':

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

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.