Loading...
  Show Posts
Pages: [1] 2 3 ... 33
1  Using Arduino / Programming Questions / Re: delay() in only one method on: January 28, 2011, 06:20:28 am
I'm relatively new to Arduino, but sorry if this is a basic question.

Is it possible to get a delay function to only affect one method and not stop my whole program from executing? (ie if I want one method to run every few seconds, but I need the rest of the program to continually execute)

A while ago on the old forum (http://arduino.cc/forum/index.php/topic,48946.msg349959.html#msg349959) I tried to explain this sort of thing in English rather than code - maybe it will help here.

Quote
Imagine you yourself want to physically ring a bell every 30 seconds. There are two ways you could do it. In the first way you ring the bell, then set an alarm clock for 30 seconds time, go to sleep, wake up when the alarm goes off, ring the bell and repeat. You are not able to do anything in that 30 seconds waiting time except wait for the alarm to go off.

The second way you would ring the bell then look at your clock and note what time it is. Then you start getting on with doing other stuff, but look at the clock periodically to see if 30 seconds has gone by yet. If not, just carry on doing other stuff and glancing at the clock. If 30 seconds has gone by, ring the bell and make a note of the new time, then carry on as before.

The first method is equivalent to to using delay() in Arduino code, the second is equivalent to using millis() to note down when you do something and wait for a time interval to pass before you do it again.

millis() tells you how many milliseconds it's been since the Arduino was powered on or reset. So you can note down what millis() returns now, do some stuff, and when you look at millis() again subtract the first reading and it will tell you how much time has gone by.

Andrew
2  Topics / Interactive Art / Kinetica art fair 2011, London, UK on: January 26, 2011, 03:43:37 pm
Just a heads up that Kinetica is back, Feb 3rd to 6th In London. I went last year and really enjoyed it - several of the exhibits were Arduino powered. Here's a taster video I did last year:



Code:
http://www.youtube.com/watch?v=n1ITYX4TqPk

and the official website is at http://www.kinetica-artfair.com

Andrew

PS Anyone know how to mention a YouTube URL without it embedding the video? I had to resort to code...
3  Forum 2005-2010 (read only) / Playground Wiki / Re: Unable to download Atachement from a wiki page on: February 06, 2010, 08:06:35 pm
That particular attachment doesn't seem to exist. The link is to upload it.

Andrew
4  Forum 2005-2010 (read only) / Forum / Re: User CP / Notifications on: August 23, 2010, 05:24:35 am
In that case, no, I never see that behaviour.

Andrew
5  Forum 2005-2010 (read only) / Forum / Re: User CP / Notifications on: August 23, 2010, 04:01:25 am
The only time I see it is when a new user happens across a long dead thread, doesn't notice it happened in 2008 and enthusiastically posts to it.

Not necessarily a bad thing of course - new info does come to light all the time.

Andrew
6  Forum 2005-2010 (read only) / Forum / Re: Forum update on: August 22, 2010, 03:48:16 pm
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1260740736/0
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1243945786/9

and probably more...

Andrew
7  Forum 2005-2010 (read only) / Forum / Re: my 2¢ on: August 04, 2010, 04:01:41 pm
Maybe code rot would be helped (or at least the potential for it flagged up) by having some standard #defines or #pragmas in sketches. Say something like

Code:
#define CPU atmega328
#define IDE 0.19

then the Arduino code pre-processor could squawk if the values didn't match with the currently selected board/CPU and IDE version.

Something for the Arduino 1.0 suggestions thread?

Andrew
8  Forum 2005-2010 (read only) / Forum / Re: my 2¢ on: August 04, 2010, 02:42:06 pm
The trouble is, the code probably was legit when someone put it in the playground, but then three versions of the IDE later there's some incompatibility and it doesn't work anymore.

Andrew
9  Forum 2005-2010 (read only) / Troubleshooting / Re: 9 DOF Razor help on: August 15, 2010, 04:32:19 am
Now you've made one post you'll be allowed to post a link to the data sheet.

Andrew
10  Forum 2005-2010 (read only) / Troubleshooting / Re: Can't find the cause of the error on: August 22, 2010, 05:03:18 pm
Well I never, I would have sworn I'd had compiler error messages from leaving in a trailing comma, and in Java too. I've just tried it in both and you're right, it works fine. Sorry about that.

Can't see why it would be good practice though...

Andrew
11  Forum 2005-2010 (read only) / Troubleshooting / Re: Can't find the cause of the error on: August 22, 2010, 03:40:42 pm
EDIT: Turns out I'm completely wrong here. See deSilva's comment below.

One problem at least is that you have a comma after the last value in your rgb[] array. It should end with
Code:
0xFF };

Same problem in your enum too.

Andrew
12  Forum 2005-2010 (read only) / Troubleshooting / Re: serial connectivity on: August 21, 2010, 08:38:31 am
Hint: http://arduino.cc/en/Reference/Assignment versus http://arduino.cc/en/Reference/If

Andrew
13  Forum 2005-2010 (read only) / Troubleshooting / Re: Multiple modes and patterns on: August 12, 2010, 03:45:46 am
Imagine you yourself want to physically ring a bell every 30 seconds. There are two ways you could do it. In the first way you ring the bell, then set an alarm clock for 30 seconds time, go to sleep, wake up when the alarm goes off, ring the bell and repeat. You are not able to do anything in that 30 seconds waiting time except wait for the alarm to go off.

The second way you would ring the bell then look at your clock and note what time it is. Then you start getting on with doing other stuff, but look at the clock periodically to see if 30 seconds has gone by yet. If not, just carry on doing other stuff and glancing at the clock. If 30 seconds has gone by, ring the bell and make a note of the new time, then carry on as before.

The first method is equivalent to to using delay() in Arduino code, the second is equivalent to using millis() to note down when you do something and wait for a time interval to pass before you do it again.

millis() tells you how many milliseconds it's been since the Arduino was powered on or reset. So you can note down what millis() returns now, do some stuff, and when you look at millis() again subtract the first reading and it will tell you how much time has gone by.

Abbreviated code snippet (won't run):

Code:
void loop() // do over and over again
{
  // Do stuff here that you want to do all the time
  ...
  // and some more stuff here
  ...

  // glance at the clock
  unsigned long currentMillis = millis();
 
  // has enough time gone by?
  if((currentMillis - previousMillis) > timeInterval) {
    // if yes, save the current time
    previousMillis = currentMillis;  

    // do something at a regular time interval
    // 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);
  }
}

Once you understand how this works for doing one thing at a regular interval (here blinking an LED on or off) you can expand it to do two different things at different intervals. Instead of just recording previousMillis you have previousMillis1 and previousMillis2, one for each of the two different things you want to do, and two different time intervals timeInterval1 and timeInterval2. Then each time round loop() you check if either interval has passed and if so do the appropriate thing.

Code:
void loop()
{
  // Do stuff here that you want to do all the time
  ...

  // glance at the clock
  unsigned long currentMillis = millis();
 
  // has enough time gone by for red action?
  if((currentMillis - previousMillisRed) > timeIntervalRed) {
    // if yes, save the current time
    previousMillisRed = currentMillis;  

    if (redLedState == LOW) {
      redLedState = HIGH;
    }
    else {
      redLedState = LOW;
    }
    digitalWrite(redLedPin, redLedState);
  }

  // has enough time gone by for green action?
  if((currentMillis - previousMillisGreen) > timeIntervalGreen) {
    // if yes, save the current time
    previousMillisGreen = currentMillis;  

    if (greenLedState == LOW) {
      greenLedState = HIGH;
    }
    else {
      greenLedState = LOW;
    }
    digitalWrite(greenLedPin, greenLedState);
  }


}

Hope this helps.

Andrew
14  Forum 2005-2010 (read only) / Troubleshooting / Re: java.lang.NullPointerException Problem on: August 04, 2010, 03:42:56 pm
Yay!  smiley
15  Forum 2005-2010 (read only) / Troubleshooting / Re: java.lang.NullPointerException Problem on: August 04, 2010, 08:27:53 am
I got a java.lang.NullPointerException when I reinstalled the Arduino IDE and tried to compile a sketch without selecting a hardware board. Make sure something (e.g. Duemilanove with ATmega328) is selected under the Tools, Board menu first.

Andrew
Pages: [1] 2 3 ... 33