Beginner---combining 2 sketches!!??

Hi, i’m trying to control 2 different circuits with one code…how do i combine them together so they occur at the same time?

I realise there must be a simpler method of coding the numeric display output but haven’t figured this out yet.

Heres some code:

i

nt pin1 = 2;
int pin2 = 3;                                    //                            --6--
int pin3 = 4;                                    //                         5 |     | 7
int pin4 = 5;                                    //                           |--4--|
int pin5 = 6;                                    //                         1 |     | 3
int pin6 = 7;                                    //                            --2--
int pin7 = 8;
int gnd1 = 11;                                 //                          gnd1 is display 1's gnd
int gnd2 = 9;                                   //                          gnd2 is display 2's gnd
int timer = 500;                               //   A timer, to run the for loop 500 times, which turns out as 1 second.
int value;                                        //   The value, part of the FADING display

void setup(){
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);           //The following sets up all of the pins for use.
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  pinMode(pin7, OUTPUT);
  pinMode(gnd1, OUTPUT);
  pinMode(gnd2, OUTPUT);
  
  
  
   for (int i=0; i<timer; i++){                     //   The for loop, for running the program 500 times.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 03 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  for (int i=0; i<timer; i++){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 02 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  for (int i=0; i<timer; i++){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 01 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  for (int i=0; i<timer; i++){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 00 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  
  
}

void loop() {
  
  
  digitalWrite(13, HIGH);   //  Turns the TIMER ENDED LED on.
  
   for(value = 0 ; value <= 255; value+=5) // fade in (from min to max) 
  { 
    analogWrite(gnd1, value);           // sets the value (range from 0 to 255) 
    analogWrite(gnd2, value);           // sets the value (range from 0 to 255) 
    delay(10);                            // waits for 30 milli seconds to see the dimming effect 
  } 
  for(value = 255; value >=0; value-=5)   // fade out (from max to min) 
  { 
    analogWrite(gnd1, value);           // sets the value (range from 0 to 255) 
    analogWrite(gnd2, value);           // sets the value (range from 0 to 255) 
    delay(10); 
  }  
  
}

Now, how do i add this in so they run at the same time:

int ledPin = 13;    // LED connected to digital pin 9

void setup()  {    // nothing happens in setup 
} 

void loop()  { 
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
}

Thanks

K

I'm not gonna try to undertsand what your code is supposed to do because it seems to be doing some nonsensical things. As such, there's no simple solution to your problem that I can suggest like running one of the loops every third iteration if the code may be altered to behave differently.

So you need some kind of a generic solution.

The reason you're having a problem is because you're explicitly specifying all of the steps, and the delays between them. You're saying "do this, pause half a millsecond, then do this this pause half a millisecond, then repeat 500 times."

But what you should be doing is putting everything in a big loop, and specifying the time at which each event should occur, and have one or more state variables for special circumstances.

In other words, in void loop(), you want to keep track of millis() or micros() which start at 0 and begin counting up when your program starts, and when they pass certain milestones, do certain things.

You could for example, trigger an event every 30 milliseconds, like your second code example has. And you could pair that with a state. So, you might have a varable Fade, which is your state, and initially you have it set to true. Then every 30 milliseconds, you eithe increment or decrement another variable FadeValue which tells you what to set the analog pin to, until FadeValue reaches 255 or 0, whichever direction your state variable Fade says you're going at the time.

Meanwhile, you can have other events trigger, and since the loop runs constantly, and no delay statements are ever actually used, these things can all run concurrently.

Oh and, I can't reccomend any of them because I haven't used them, but there seem to be some libraries already out there to help you with timing and triggering events at certain times. If you find it too difficult to manage your own timing functions maybe these will help:

http://www.arduino.cc/playground/Code/EventFuse http://www.arduino.cc/playground/Code/Metro http://www.arduino.cc/playground/Main/MsTimer2

Ps: I'm new to the Arduino myself, so there might be better ways of doing things. I come from a background of programming games, and keeping track of the time and various states and how much time has passed between updates is what comes naturally to me. But maybe interrupts are a less power-hungry way of doing things on a microcontroller. I dunno.

Let me just clarify the abover a bit.

Let’s say, after 10 seconds, you want to start fading your display out, and you want the fade to last 2 seconds.

You might, in your main loop do something like this:

fadestart = 10000
fadetime = 2000
fadeend = fadestart + fadetime

if (millis() >= fadestart) && (millis() < fadeend) {
fadevalue = constrain(255.0 * (1.0 - (millis()-fadestart)/fadetime), 0, 255)
analogWrite(ledpin, fadevalue);
}

You can get a lot more complicated than that, having, say a global state which you can put in a switch statement to use to fade in, display 1, display 2, display 3, then fade out, in turn, but that’s the basics.

Btw, another way of keeping track of time is to have an accumulator and add the amount of time which has passed since the previous loop to the accumulator each time through. So it 10 nanoseconds has passed, you add that and when 500 nanoseconds has accumulated, you do something, then either never do it again, or reset it and wait till a boolean is set to true to do it again, or just reset it to 0 and let it repeat the action forever.