How to combine two codes and make them work


so new arduino. making led lights first responders , need this. have code uses 2 buttons. click 1 , led comes on, click other , led turns off. have code make several leds flash randomly. trying insert sed code first code when press first button, leds use flash code, , when press second button, cuts off. can appreciated!


button code:

code: [select]
const int buttonpin[] = {2,3};     // number of pushbutton pins
const int ledpin =  12;      // number of led pin

// variables change:
int buttonstate = 0;         // variable reading pushbutton status

void setup() {
  // initialize led pin output:
  pinmode(ledpin, output);     
  // initialize pushbutton pin input:
  for(int x=0; x<2; x++)
  {
    pinmode(buttonpin[x], input);
  } 
}

void loop(){
  // read state of pushbutton value:
  for(int x=0; x<2; x++)
  {
    buttonstate = digitalread(buttonpin[x]);

    // check if pushbutton pressed.
    // if is, buttonstate high:
    if (buttonstate == high && buttonpin[x] == 2) {   
      // turn led on:   
      digitalwrite(ledpin, high);
    }
    if (buttonstate == high && buttonpin[x] == 3) {
      // turn led off:
      digitalwrite(ledpin, low);
    }
  }
}]



flash code:

#define numberoflights 16

byte pins[] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
byte pinstate[numberoflights];
long int changetime[numberoflights];
int flashrate[numberoflights];
long flashchange;   // how change flashing patterns

void setup() {
  for(int = 0; i< numberoflights; i++) {
    pinmode(pins[i], output);
    changetime[i] = millis() + random(1000, 200);
    pinstate[i] = low;
  }
  setflashtime();
}

void loop() {
  for(int = 0; < numberoflights; i++) {
   if(changetime[i] <= millis()) {
   pinstate[i] = ~pinstate[i];
   digitalwrite(pins[i], pinstate[i]);
   changetime[i] = millis() + flashrate[i];
   }
  }
  if(flashchange <= millis()) setflashtime();
}

void setflashtime(){
  for(int i=0; i<numberoflights; i++){
    flashrate[i] = random(100, 500);
  }
  flashchange = millis();  // next time change pattern
}]


i have attached files if helps.

have @ merge code demo.

...r


Arduino Forum > Using Arduino > Programming Questions > How to combine two codes and make them work


arduino

Comments