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.
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.
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:
#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
//...
//...
}
}
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...
You can see this example too. It can also be found in your Arduino software
File>Examples>Digital>BlinkWithoutDelay
/* 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);
}
}
NFA_Fabrication:
... 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.
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.
int ledPin = 13; // the number of the LED pin
int ledPin1 = 12;
int buttonPin = 7;
int ledState = LOW; // ledState used to set the LED
int ledState1 = 0;
int buttonState = 0;
unsigned long previousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to blink (milliseconds)
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
if (currentMillis - previousMillis >= interval) // save the last time you blinked the LED
{
previousMillis = currentMillis;
if (ledState == LOW)
{
ledState = HIGH;
}
//if (buttonState == HIGH)
{
//delay(10000);
//digitalWrite(ledPin1, HIGH);// turn LED on:
// delay(1000);
}
}
else
{
ledState = LOW;
}
digitalWrite(ledPin, ledState); // set the LED with the ledState of the variable:
}