Pages: [1]   Go Down
Author Topic: Run Two Sketches Simultaneously?  (Read 1154 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am new to this, and have been searching for a solution with no resolve. I am installing some programmable lighting on an RC plane and have no problem with the hardware aspect, but the programming is getting a bit over my head. I have been modifying the basic "Blink" code with reasonable success, but would like to write a few different sketches that run simultaneously so that I can have different independent blinking circuits. And it would be also awesome if I could turn on/off independent sketches by pulling an input low. So it basically comes down to these two ?'s:

1.) Is there any example code out there with 2 or more sketches running at the same time?

2.) Is there a way to turn individual sketches on/off by pulling one of the analog inputs low?

I attached the code I have so far (Just the 1 sketch). It basically runs some red and blue lights simulating "Police Lights" inside the cowling vents on my plane. I want to run wingtip and top of the tail lights that I want to blink at a different rate.

* Cop Lights Text.txt (1.79 KB - downloaded 12 times.)
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4689
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This page should help very much. It's clear and simple:

http://www.gammon.com.au/forum/?id=11411

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No on the multiple sketch. You can have multiple loop but that's a bit advanced.
You can do blink without delay. Just have a few of those conditions
http://arduino.cc/en/Tutorial/BlinkWithoutDelay

And you can use pwm to fade LEDs in and out
http://arduino.cc/en/Tutorial/Fading

Everything you need is on the site. Just do a little searching.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am new to this, and have been searching for a solution with no resolve.

I might try some basic programming tutorials, e.g. http://www.cplusplus.com/doc/tutorial/

Quote
1.) Is there any example code out there with 2 or more sketches running at the same time?

No, because there can be only one sketch loaded into the microcontroller at a time.

Quote
2.) Is there a way to turn individual sketches on/off by pulling one of the analog inputs low?

Given that the answer to the first question is no, so is the answer to this question, BUT it's entirely possible to code a sketch to do two (or more) different things based on inputs. I might use a digital input, high and low being a binary sort of thing rather than analog, but here is a very basic framework:

Code:
#define CONTROL_PIN 8    //an input pin that controls what happens

void setup(void)
{
    pinMode(CONTROL_PIN, INPUT);
}

void loop(void)
{
    if (CONTROL_PIN == HIGH) {
        //do one thing
        //...
        //...
    }
    else {
        //do something else
        //...
        //...
    }        
}
« Last Edit: March 24, 2013, 10:51:52 pm by Jack Christensen » Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Sr. Member
****
Karma: 2
Posts: 344
YES! It is possible. Even Impossible Says I m Possible. :)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You actually dont need to run 2 or more sketches at a time. You can perhaps use timer interrupts....
Suppose you have 3 LEDs & you can modify the blink so that led 1 ,2 & 3 will blink after .... say 0.7, 0.8. 0.9 seconds interval...

http://www.gammon.com.au/forum/?id=11488

http://www.engblaze.com/microcontroller-tutorial-avr-and-arduino-timer-interrupts/

You can see this example too. It can also be found in your Arduino software
File>Examples>Digital>BlinkWithoutDelay

Code:
/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
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 = 1000;           // 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);
  }
}
Logged

"Imagination is more important than knowledge."
-Einstein

Offline Offline
Faraday Member
**
Karma: 57
Posts: 2764
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You cannot have two sketches running at the same time.
In addition to the other suggestions,   you always have the option to have two arduinos.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

... would like to write a few different sketches that run simultaneously so that I can have different independent blinking circuits.

Certainly you can have different blinking circuits. My page (linked to further up) explains how to do that. Plus there are various other tutorials around.

http://www.gammon.com.au/blink


Let me give you an analogy ... you only have one brain, right? But you can do multiple things? If you were in a room with some light switches I'm sure you could turn them on and off at different rates. Well, translate the method you - with one brain - do that, to a computer program.
Logged

Pages: [1]   Go Up
Jump to: