Adjusting dimmer

Hi,
I'm trying to make a dimmer circuit that just makes a led fade in at a rate set by a potentiometer and them fade out at the same rate.
Whenever i have tryed to code this i keep getting errors.
Any help would be great.
Cybot

Hi Cybot, don't be shy, why not post your code.

Ive tryed to make it using code from examples so please don't tell me thats they are described wring just trying to get it to work.

/*
* Cybot's attempt
*/

int value = 0;                            // variable to keep the actual value 
int ledpin = 9;                           // light connected to digital pin 9
int potPin = 2;                           // Potn

void setup() 
{
int value = 0;                            // variable to keep the actual value 
int ledpin = 9;                           // light connected to digital pin 9
int potPin = 2;                           // Potn
} 

void loop() {
int value = 0;                            // variable to keep the actual value 
int ledpin = 9;                           // light connected to digital pin 9
int potPin = 0;                           // Potn
  pinMode(2, OUTPUT);
potpin = analogRead(2);    // read the value from the sensor
  for(value = 0 ; value <= 255; value+=potpin) // fade in (from min to max) 
    analogWrite(ledpin, value);           // sets the value (range from 0 to 255) 
  delay(100);                            // 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);  
}

Cybot

Cybot, your code will compile if you change potpin to potPin in the places you use it in loop.

And I think you need to put brackets around your for loop. The code posted above will not execute the delay until after the for loop has completed.
for(value = 255; value >=0; value-=5){ // fade out (from max to min)
analogWrite(ledpin, value);
delay(30);
}

Have fun!

Thanks very much works great :slight_smile:
This is for a friends electronics assignment.
:slight_smile:

I am not sure which is worse, me doing another persons homework, or me helping someone else do another persons homework :wink:

For extra credit, he may want to study the code and see why potPin may not be a good name for the variable that is assigned the value from analogRead :wink:

Hi,
His homework was to find a usefull project that someone could build. He was looking for this script but could not find one so we tried to make one.
His teacher like the fact that he had made something new and had asked for help. :slight_smile:
I was helping because i have an Arduino and he doesn't so he could not test it.