Arduino interrupts()

Hi... I'm going to buy my first arduino...

what i want is to make a robot which avoid obstacle for now, and at the end of the loop test the brightness of the room... so I am going on with a pseudocode

here it is:

void setup

void loop() { analyze and avoid obstacles; test senzor if light is low start blinking 6 LEDs }

but i want these leds to be blinking all the way until it runs... the robot... only after the loop end point it is tested.... example...

the robot is out... making his way... enters the house... it's dark... he starts blinking the LEDs... so the LED blinks all the way in the house... (the robot will test the light in the room but is still dark)... LED's are still blinking, the robot is avoiding obstacles... he goes out.... there is light... he shuts down the blinking LEDs...

so theoretically, it runs a void blinking() and void loop() together... could this be made with interrupts()? if so please tell me how...

Kinda resembles the blinking without a delay check it out


not really what I want...

I want to blink it continously, better said fade it in and out continously, until it gets a better light...

that comes to the same thing you mix the fading example with blink without delay loosing the delay(30) from the code and use a counter in stead like the counter inblink without delay.

You really dont need to use interrupts for this, unless it's critical timing between detection of light and stopping fading the led. you just check the pin for lightsensor.


can you write me a sketch? I will change values later when my Duemilanove arrives

just google one up, I do not have the time

here is one example,

this is edge detection and fading


that just turn on the level of the led... interrupts() sound better for me, but I need to learn it...

I think that I didn't explained the things verry well to be clear for everybody :(

Always great to learn things


now thats what I call advanced :) don't understand anything... by the way... i don't used arduino ever, and I don't have one yet... so until I don't get the arduino :| I won't be able to test it and use it

Hmmm..... :)

Just remember, keep it SIMPLE, but not TOO simple you do not want to code few hundred lines when ten will do the trick.

From your description of the project, you do not need interrupts, but as a proof of concept for coding with interrupts it might do.

If you need challenge, write the assembled HEX file by hand ;)


HEX by hand? what is HEX? as I told... I'm at a verry beginning

From the Arduino PLAYGROUND:

A number of things have to happen for your Arduino code to get onto the Arduino board. First, the Arduino environment performs some small transformations to make sure that the code is correct C or C++ (two common programming languages). It then gets passed to a compiler (avr-gcc), which turns the human readable code into machine readable instructions (or object files). Then, your code gets combined with (linked against), the standard Arduino libraries that provide basic functions like digitalWrite() or Serial.print(). The result is a single Intel hex file, which contains the specific bytes that need to be written to the program memory of the chip on the Arduino board. This file is then uploaded to the board: transmitted over the USB or serial connection via the bootloader already on the chip or with external programming hardware.

Basicly human compiler :) (you must enter genius-mode first) Might consider Lawnmower man drugs ( ) Not to be recomended ;D


hehe... so get a keyboard with this keys: 0 and 1 :D

btw... so i thought now this way...

use a digital pin and an analog pin...

analog - light senzor digital - power for 4 leds... (lets say this way)... ok

so when the light is down, leds turn on and vice-versa...

but what if, when the digital pin is high, the current goes to a circuit, tranform 5V to 12V and there to be rezistors, tranzistors etc to blink those LEDs smoothly? :)

if it can be made... please show me a diagram...

I don't want fast and a lot of blinkings... I want smooth slow blinks :)

LEDs are made to blink with PWM, you just see the fading, you see it as you have dimmed ordinary bulb with a dimmer

The speed of pwm is about 490Hz and the eye can not see any blinking over 30 Hz and the eye “sees” only the avarage number of photons reaced to the eye during the blinking. Hence apparent dimming of the LED.

So the link I sent you for edge detection will work for you

if (millis() - lastFadeTime > 5)
Here is the wait time to dimm the LED one level 5ms
So from totally bright to dark takes 1.2 seconds
And belive me you can hardly see the difference between one dimming level. so longer delay can easily be made. I have faded my leds over a minute and I did not see it “step” in brightness,to me it was continious dimming and brightening.

// This example uses a digital input to control a fading LED. 
// The LED turns on when the switch goes from off to on, then fades slowly to black.
// Â  It illustrates two principles:
//  the idea of edge detection or state change detection, and the idea of time delay without using delay().

int lastState = 0;          // last state of the switch
int currentState = 0;     // current state of the switch
int ledLevel = 0;           // level of the LED analogWrite
long lastFadeTime = 0;  // last time you decremented the LED level

void setup() {
  pinMode (2, INPUT);       // the switch pin
  pinMode(6, OUTPUT);     // the LED pin
  Serial.begin(9600);        // initialize serial communication

void loop() {
  // read the switch:
  currentState = digitalRead(2);

  // if the switch has changed, do something:
  if (currentState != lastState) {
    // if the switch is high:
    if (currentState == 1) {
      // set the LED level
      ledLevel = 255;
      Serial.println("turned on");
    // save the current state of the switch as the last state for next time through:
    lastState = currentState;

  if (millis() - lastFadeTime > 5) {
    // fade the LED level:
    if (ledLevel >0 ) {
      // turn on or off the LED
      analogWrite(6, ledLevel);
      // decrement the LED level:
    else {
      // turn the LED off:
      digitalWrite(6, LOW);
    // note the last time you faded the LED:
    lastFadeTime = millis();

Start to do the led examples that come with the IDE with the arduino before assuming things. This sketch is almost doing what you ask.
The only difference is that “currentState = digitalRead(2);” is reading a state from a digital pin, so by changing this statement to “currentState = analogRead(9);” and then in the loop “if (currentState < Lowest_light_level) {” where Lowest_light_level is a variable that holds your threshold value of what you call lights_on in the room. Add code to increase the level of LED brightness again and reset the LastFadeTime timer. Now the sketch is doing what you ask.

skematic for hooking up leds

use a 5v transformer where there says ARDUINO+5V, not the arduino 5V pin, as the arduino cannot supply power more than 40mA on one pin without damage to the arduino.

ps. 8bit hex is like
00 45 3A FF 3B 57…
My name in hex: 44 61 76 69 64
and Binary: 01000100 01100001 01110110 01101001 01100100