Two separate pieces of code on one arduino

I've got two pieces of code that work. One is a light sensor that puts two railway coaches lights on then a station kiosk light when its dark enough. I've got a delay between each of them. This all works fine.

Next I've got a bit of random code to flicker two yellow LED's and one red LED and I've put them in the fire box of a small model kit loco. This also works fine and looks really nice.

My problem is the fire box flicker works ok on its own but when I add it to the lighting program it does what I expected; flickers then goes solid whilst the rest of the code runs then flickers etc. I'm thinking this is because the flicker code runs then stops whilst the light code runs. Is there any way to run the two codes on one arduino and still have a constant flicker? I've tried all sorts (limited knowledge) but can't get what I want. Any ideas?

Hi

See the Merge Two Codes tutorial:
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

Yes that is the problem.

Yes.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

Thanks for the links, I got it to , almost, work.

I've got the two sets of codes called in the void loop () section as per the example and the lights coming on and going off work fine. The flickering lights do come on but they're steady not flickering. It's as if its stopped using the random code. If I comment out the code for the lights on and off the flickering resumes. I feel like I'm almost there!

I'm not a coder and the link to Robin2's example was something I could understand. The links to the examples on thebox have almost fried my head. I think it's time to have a break and go and watch some footie!

1 Like

If you post your sketch, people might be able to advise :wink:

1 Like

I think you are using the Delay() function, if so learn how to use the mills function in a non blocking mode. When you solve that your code will probably work.

Yes, you're right gilshultz, I am using several delays in the code for the lights. Ive actually tried running the program with the delays all commented out but it made no difference. I have noticed that the LED's that are supposed to flicker do actually change from time to time but it is very slowly.

I'll post the code I'm using as soon as I remember how to do it on this forum.

//LED light sensor for coach and kiosk & Loco firebox flames flicker
//Wiring diagram photos in this codes folder
//Constants
const int LIGHT_SENSOR_PIN = A0; // Arduino pin connected to light sensor's  pin
const int LED_PIN_1          = 6;  // Arduino pin connected to Mk 3 Coach LED's pin
const int LED_PIN_2          = 3;  // Arduino pin connected to Kiosk LED's pin
const int LED_PIN_3          = 7;  // Arduino pin connected to Mk 2 Coach LED's pin
//const int LED_PIN_4          = 12; // unused as yet
const int ANALOG_THRESHOLD = 300;//Original 500. 300 seems best
const int ledPin1 = 9;
const int ledPin2 = 10;
const int ledPin3 = 11;
//Variables
int analogValue;

//Run once at startup
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(LED_PIN_1, OUTPUT); // set arduino pin to output mode
  pinMode(LED_PIN_2, OUTPUT); // set arduino pin to output mode
  pinMode(LED_PIN_3, OUTPUT); // set arduino pin to output mode
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);

}

//Run continuously until ending criteria if any met
void loop() {
  ledFlicker();
  lightsOn();
}

//================
void ledFlicker() {
  {
    analogWrite(ledPin1, random(120) + 135); //yellow
    analogWrite(ledPin2, random(120) + 135); //red
    analogWrite(ledPin3, random(120) + 135); //yellow
    delay(random(100));
  }
}

//==================
void lightsOn() {
  int analogValue = analogRead(A0);  // reads the input on analog pin A0 (value between 0 and 1023)

  Serial.print("Analog reading: ");
  Serial.print(analogValue);   // the raw analog reading

  // Light values to write out to the Serial Monitor
  if (analogValue < 10) {
    Serial.println(" - Dark");
  } else if (analogValue < 200) {
    Serial.println(" - Dim");
  } else if (analogValue < 500) {
    Serial.println(" - Light");
  } else if (analogValue < 800) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
  }
  //Coaches and Kiosk lights on/off
  delay(500);
  analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin
  delay(2000);
  //Mk3 Coach Lights
  if (analogValue < ANALOG_THRESHOLD) {
    digitalWrite(LED_PIN_1, HIGH);// turn on LED
  }
  else {
    digitalWrite(LED_PIN_1, LOW);  // turn off LED
  }
  delay(1000);
  //Mk2 Coach Lights
  if (analogValue < ANALOG_THRESHOLD) {
    digitalWrite(LED_PIN_3, HIGH);// turn on LED's
  }
  else {
    delay(2000);
    digitalWrite(LED_PIN_3, LOW);  // turn off LED's
  }
  delay(1000);
  // Kiosk Lights
  if (analogValue < ANALOG_THRESHOLD) {
    digitalWrite(LED_PIN_2, HIGH);// turn on LED's
  }
  else {
    delay(2000);
    digitalWrite(LED_PIN_2, LOW);  // turn off LED's
  }
}

I hope thats posted correctly!

As long as you have delay calls in your code you will never get it working.

You have been told how to solve your problem in post#3 but you do not seem to get it.

Please explain what points you don't understand.

Just time to try things out I'm afraid. I was looking for a quick fix but hopefully next week I'll get a bit of time to myself to go through your tutorial properly. I like to see if I can do things myself first. As a note I did try commenting out all the delays but it made no difference.

Yep, you're right, I've just gone through my code again and realised I'd missed commenting one of the delays out. Now they're all commented out it all works. Lights on the coaches and kiosk come on straight away but I'll go through the tutorial to see if I can get it to work how I want.

Hello phil_lawson

I like model railways and the possibilities of using an Arduino for lighting.

That's why I wrote a little sketch in C++.

Included are two arrays

constexpr byte FlickerLeds[] {9,10,11};

and

constexpr byte KioskCoachLeds[] {2,3,4};

These two arrays contain the addresses for the pin addresses used.

Using the Range-based for loop instruction,

for (auto KioskCoachLed:KioskCoachLeds)

and

for (auto FlickerLed:FlickerLeds)

all elements from the array are processed one after the other.

Check it and have fun with your model railways.

constexpr byte FlickerLeds[] {9,10,11};
constexpr byte KioskCoachLeds[] {2,3,4};
constexpr byte DayLightPoti {A8};
constexpr int ANALOG_THRESHOLD = 300;

void setup() 
{
  for (auto FlickerLed:FlickerLeds) pinMode(FlickerLed,OUTPUT); 
  for (auto KioskCoachLed:KioskCoachLeds) pinMode(KioskCoachLed,OUTPUT); 
}

void loop()
{
  for (auto KioskCoachLed:KioskCoachLeds) digitalWrite(KioskCoachLed,analogRead(DayLightPoti)>=ANALOG_THRESHOLD ); 

  static unsigned long stamp;

  if (millis()-stamp >= (unsigned long) random(100))
  {
    stamp=millis();
    for (auto FlickerLed:FlickerLeds) digitalWrite(FlickerLed,random(120) + 135); 
  }
}
1 Like

Well I got your code to work Paul, I had to change A8 to A0 and the > to < for the threshold, but unfortunately the LED's don't flicker. It's much neater than what I have though.

I'll still look at Grumpy_Mike's tutorial to see if I can get it to work that way.

Post the sketch to see what happens

Hi Paul, not quite sure what you're asking. Do you want me to post the sketch I'm using? It's pretty much yours with the minor alterations. I've already tried it on my arduino.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.