I need assistance with a project.

I am brand new to the Arduino. I literally bought my board about a half hour ago and I need a starting point. The gist of m project is when I open up my trunk, I'm going to want an led to slowly turn on and then slowly turn back off when the trunk is closed. I have the bored, some led's, and a large momentary push button. I know it's going to be using an if then statement, but how exactly would I set up the code, and how exactly should I set up the button on the board? Thank you for any help you guys can provide in advance.

Try getting an LED to blink first.

Then look around the other tutorials, add a switch and so on.

Hi Frank, and welcome to the wonderful world of Arduino.

I suggest that you start off by looking through some of the code examples on this site and build up some sketches and circuits. There are a lot of good books, too, that you should buy or borrow.

I caution you from jumping in too quickly and asking the members on the forum to draw your circuits and write your code for you. The fun is in the learning!

Try some things and ask for help when something doesn't work. You'll find a lot of help here!

Pat.

After playing with the blink example (and a few others) look at "blink without delay" . You can't really combine blink with a button but you can combine a button with "blink without delay". You will also need to look at "de-bouncing". Look in the "playground" section of the main site.

Good luck

Mark

Thanks guys! I'll definitely mess around with it for a little while. I'm actually in school now for programming and have some past knowledge on C++. I'm not an expert, but I know some. I'll ask some questions as they come. This already seems to be a pretty helpful forum.

Alright, i've got the LED blinking an i've been messing with it. I have that down, now I need to ask a few questions. Is it possible to accomplish what I want without a bread board? and if so, what ports would I connect the push button too and how exactly would that be coded? This is the example i'm using.

const int ledPin =  13;     

int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 50;           // 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);
  }
}

FrankRodelli:
when I open up my trunk, I'm going to want an led to slowly turn on and then slowly turn back off when the trunk is closed.

This reminds me of the age-old question about fridge lights. If the light only fades off when the trunk is closed, how will you know? This might make more sense as a way to control the car's interior light based on door switches.

Well, yeah :stuck_out_tongue: I just want it to slowly come on. I don't really need it to fade out. The issue is, my trunk doesn't have a light. I'd like to set it up with a switch because there are no sensors in my trunk to indicate when It's open and the odds are when I am opening my trunk, the rest of the door will be closed therefore the light would not come on. Is there a simple way to lets just say I hooked my led up to point a and b and my switch to c and d and the code would say if c and d= 1 (connected) then a +b =1(led on) Or is that not possible?

FrankRodelli:
Well, yeah :stuck_out_tongue: I just want it to slowly come on. I don't really need it to fade out. The issue is, my trunk doesn't have a light. I'd like to set it up with a switch because there are no sensors in my trunk to indicate when It's open and the odds are when I am opening my trunk, the rest of the door will be closed therefore the light would not come on. Is there a simple way to lets just say I hooked my led up to point a and b and my switch to c and d and the code would say if c and d= 1 (connected) then a +b =1(led on) Or is that not possible?

But what value does the arduino bring to the party? Why not a simple tilt switch mounted to the trunk lid wired to the lamp and on to +12vdc. Seems a waste of an arduino board if that is all the application requires.

Lefty

https://www.sparkfun.com/products/10289?

The reason i'm doing this in the first place is because there is NO lamp in my trunk. Instead of installing a plain light, I wanted to make an aesthetically pleasing LED arrangement to complement my subs. This is where I am. I think this should be working. I've got the LED hooked up to the GND and 13. I have a mommentary push button hooked up one end on the 5 volt spot, and the other on the #2 spot. This is what the code looks like.

const int buttonPin = 2;     
const int ledPin =  13;      


int buttonState = 0;        

void setup() {
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {        
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    digitalWrite(ledPin, LOW); 
  }
}

It's really unpredictable now. It will work sometimes, and others it flicker or stay on and the button won't turn it off. Should the button be grounded? What's going on?

Do you have resistors wired in?

You can use the LED on board at pin 13 without resistors, others need resistors wired along with the lamp.

If you don't have any resistors with your switch you're doing it wrong. You can use a switch without resistors by wiring it between a digital pin and ground then turning on the internal pullup resistors. Then HIGH is off and LOW is on.

Without resistors you could be damaging your Arduino.

Thanks for the heads up! So, how exactly would I wire the button and use the built in resistor?

I have a mommentary push button hooked up one end on the 5 volt spot, and the other on the #2 spot.

That is probably the root of your problem. You will have a 'floating input pin' condition when you release the switch. This causes the pin 2 digitalRead() statements to return random bad values when not being pressed. Possible fixes:

  1. You need to wire a pull-down resistor say 10k ohms from pin 2 to ground which will force an electrical low onto the pin when you release the switch.

Or
2. Wire one end of the switch to an arduino ground pin and the other end to pin 2. Then in your setup code add the statement digitalWrite(buttonPin, HIGH); right after your existing pinMode(buttonPin, INPUT); That will turn on the internal pull-up resistor for pin 2. Then in your code make any change needed because now a digitalRead() = LOW means the switch is being pushed and = HIGH means the switch is not being pressed.

Lefty

Thank you so much man! God, this is really one of the most helpful communities I've been a part of already! Now to make it slowly turn on. I'll post again if I run into anymore trouble.

FrankRodelli:
Thank you so much man! God, this is really one of the most helpful communities I've been a part of already! Now to make it slowly turn on. I'll post again if I run into anymore trouble.

Well I'd actually like you to see also post when you have good things to report not just your problems. :smiley:

Lefty

retrolefty:

FrankRodelli:
Thank you so much man! God, this is really one of the most helpful communities I've been a part of already! Now to make it slowly turn on. I'll post again if I run into anymore trouble.

Well I'd actually like you to see also post when you have good things to report not just your problems. :smiley:

Lefty

Alright, now that it's working and I've tried on my own to make it slowly turn on and have failed, I'm turning to you again. I am assuming my code is far off. I don't know exactly how to make this argument. Here's what I got! I'l post a picture or video once I've got it installed!

Ok not tested, but it compiles OK. Note that we are going to gradually turn on the led from full off to full on via PWM control of the led using analogWrite() commands. But pin 13 isn't one of the available PWM output pins, so we have to move the led to pin 10, and be sure to use a series resistor with the led. You can increase or decrease the speed of the ramping up of the LED by changing the value inside the delay(10) command.

Good luck;

Lefty

const int buttonPin = 2;     
      
int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10


int buttonState = 0;        

void setup() {
  pinMode(PWMpin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {        
    
      for (int i=0; i <= 255; i++){
           analogWrite(PWMpin, i);
           delay(10);
       }  
  } 
  else {
    digitalWrite(PWMpin, LOW); 
  }
}