led and siren

hey I was hopeing some one could join the 2 codes 2 gether please I can not work it out secong post at bottom of page thx

{added code tags, all fixed - Moderator}

/*This code is developed by Raebel Christo.
* The code is made for anyone who wishes to make police lights
* for projects or just for fun can use this and edit as they wish.
* If you are publishing this on youtube, it would be nice to credit it
* and finally subscribe youtube channel 'Raebel Christo' for more fun
* DIY arduino projects.
* 
*/


void setup(){
 pinMode(13,1); // Blue LED on pin 13
 pinMode(12,1); //  "    "  "   "  12
 pinMode(11,1); //  "    "  "   "  11
 pinMode(10,1); // White "  "   "  10
 pinMode(9,1); //   "    "  "   "  9
 pinMode(8,1); // Red    "  "   "  8
 pinMode(7,1); //   "    "  "   "  7
 pinMode(6,1); //   "    "  "   "  6
}

void loop(){
 for(int i=0;i<15;i++){
 for(int i=0;i<5;i++){
 digitalWrite(12,1);
 digitalWrite(13,1);
 digitalWrite(11,1);
 digitalWrite(10,1);
 digitalWrite(9,0);
 digitalWrite(8,0);
 digitalWrite(7,0);
 digitalWrite(6,0);
 delay(50);
 digitalWrite(12,0);
 digitalWrite(13,0);
 digitalWrite(11,0);
 digitalWrite(10,0);
 digitalWrite(9,0);
 digitalWrite(8,0);
 digitalWrite(7,0);
 digitalWrite(6,0);
 delay(50);
 }
 delay(50);
 for(int i=0;i<5;i++){
 digitalWrite(12,0);
 digitalWrite(13,0);
 digitalWrite(11,0);
 digitalWrite(10,0);
 digitalWrite(6,1);
 digitalWrite(9,1);
 digitalWrite(8,1);
 digitalWrite(7,1);
 delay(50);
 digitalWrite(12,0);
 digitalWrite(13,0);
 digitalWrite(11,0);
 digitalWrite(10,0);
 digitalWrite(9,0);
 digitalWrite(8,0);
 digitalWrite(7,0);
 digitalWrite(6,0);
 delay(50);
 }
 delay(50);
 }
 for(int i=0;i<50;i++){
   digitalWrite(12,1);
 digitalWrite(13,1);
 digitalWrite(11,1);
 digitalWrite(10,1);
 digitalWrite(6,0);
 digitalWrite(9,0);
 digitalWrite(8,0);
 digitalWrite(7,0);
 delay(100);
 digitalWrite(12,0);
 digitalWrite(13,0);
 digitalWrite(11,0);
 digitalWrite(10,0);
 digitalWrite(6,1);
 digitalWrite(9,1);
 digitalWrite(8,1);
 digitalWrite(7,1);
 delay(100);
 }
}

int i=0;
void setup() {
 pinMode(9, OUTPUT);
}

void loop() {
 for(i=700;i<800;i++){
 tone(9,i);
 delay(15);
 }
 for(i=800;i>700;i--){
 tone(9,i);
 delay(15);
 }
 }

What's this stuff at the end supposed to be doing?

int i=0;
void setup() {
 pinMode(9, OUTPUT);
}

void loop() {
 for(i=700;i<800;i++){
 tone(9,i);
 delay(15);
 }
 for(i=800;i>700;i--){
 tone(9,i);
 delay(15);
 }
 }

Nested for loops with the same variable?

void loop()
{
    for(int i=0;i<15;i++)
    {
        for(int i=0;i<5;i++)
        {
            digitalWrite(12,1);

            .
            .
            .

Ok, try this. I put the stuff from the 2nd setup() into the first setup(), and the stuff from the 2nd loop() into the first loop().
Then deleted the extra stuff.
It compiles, I don't know if it does what you want. Try it and see.

/*This code is developed by Raebel Christo.
  The code is made for anyone who wishes to make police lights
  for projects or just for fun can use this and edit as they wish.
  If you are publishing this on youtube, it would be nice to credit it
  and finally subscribe youtube channel 'Raebel Christo' for more fun
  DIY arduino projects.


*/
int i = 0;


void setup() {
  pinMode(13, 1); // Blue LED on pin 13
  pinMode(12, 1); //  "    "  "   "  12
  pinMode(11, 1); //  "    "  "   "  11
  pinMode(10, 1); // White "  "   "  10
  pinMode(9, 1); //   "    "  "   "  9
  pinMode(8, 1); // Red    "  "   "  8
  pinMode(7, 1); //   "    "  "   "  7
  pinMode(6, 1); //   "    "  "   "  6


  pinMode(9, OUTPUT);
}


void loop() {
  for (int i = 0; i < 15; i++) {
    for (int i = 0; i < 5; i++) {
      digitalWrite(12, 1);
      digitalWrite(13, 1);
      digitalWrite(11, 1);
      digitalWrite(10, 1);
      digitalWrite(9, 0);
      digitalWrite(8, 0);
      digitalWrite(7, 0);
      digitalWrite(6, 0);
      delay(50);
      digitalWrite(12, 0);
      digitalWrite(13, 0);
      digitalWrite(11, 0);
      digitalWrite(10, 0);
      digitalWrite(9, 0);
      digitalWrite(8, 0);
      digitalWrite(7, 0);
      digitalWrite(6, 0);
      delay(50);
    }
    delay(50);
    for (int i = 0; i < 5; i++) {
      digitalWrite(12, 0);
      digitalWrite(13, 0);
      digitalWrite(11, 0);
      digitalWrite(10, 0);
      digitalWrite(6, 1);
      digitalWrite(9, 1);
      digitalWrite(8, 1);
      digitalWrite(7, 1);
      delay(50);
      digitalWrite(12, 0);
      digitalWrite(13, 0);
      digitalWrite(11, 0);
      digitalWrite(10, 0);
      digitalWrite(9, 0);
      digitalWrite(8, 0);
      digitalWrite(7, 0);
      digitalWrite(6, 0);
      delay(50);
    }
    delay(50);
  }
  for (int i = 0; i < 50; i++) {
    digitalWrite(12, 1);
    digitalWrite(13, 1);
    digitalWrite(11, 1);
    digitalWrite(10, 1);
    digitalWrite(6, 0);
    digitalWrite(9, 0);
    digitalWrite(8, 0);
    digitalWrite(7, 0);
    delay(100);
    digitalWrite(12, 0);
    digitalWrite(13, 0);
    digitalWrite(11, 0);
    digitalWrite(10, 0);
    digitalWrite(6, 1);
    digitalWrite(9, 1);
    digitalWrite(8, 1);
    digitalWrite(7, 1);
    delay(100);
  }
  for (i = 700; i < 800; i++) {
    tone(9, i);
    delay(15);
  }
  for (i = 800; i > 700; i--) {
    tone(9, i);
    delay(15);
  }
}

the bit at the bottom I need joined into the code so I can have the lights and siren noise but everytime I join them it comes up with an error

can you get it to play the siren noise and have the lights is what im trying to do

Not I, that's fancier than I can manage.

Blackfin:
Nested for loops with the same variable?

Nowt wrong with that. Might not be a great practice for we humans to keep track of as we read the code, but the uC seems to have no trouble keeping track of the scope.

From quick test sketch both using "i":

Outer 0
  Inner 0
  Inner 1
Outer 1
  Inner 0
  Inner 1
Outer 2
  Inner 0
  Inner 1

OP, see if this works for you. Compiles but not tested. A little fuzzy in the head so it's probably wonky...

Had to move the siren to pin 5 as pin 9 was already used for white#2. Not sure if that'll work for tone or not. If not, swap the W#2 to pin 5 and the siren to pin 5 and change the constants below to match.

const byte pinB1 = 13;
const byte pinB2 = 12;
const byte pinB3 = 11;
const byte pinW1 = 10;
const byte pinW2 = 9;
const byte pinR1 = 8;
const byte pinR2 = 7;
const byte pinR3 = 6;

const byte pinSiren = 5;

const byte grpinLights[] = 
{
    pinB1, pinB2, pinB3,
    pinW1, pinW2,
    pinR1, pinR2, pinR3
};

void setup()
{
    for( int i=0; i<8; i++ )
        pinMode( grpinLights[i], OUTPUT );

    pinMode( pinSiren, OUTPUT );
    
}//setup

#define INCREASING      0
#define DECREASING      1
void SirenStateMachine( void )
{
    static byte
        stateSiren = INCREASING;
    static unsigned int
        freqSiren = 700;
    static unsigned long
        timeSiren = 0;

    if( millis() - timeSiren < 15 )
        return;
    timeSiren = millis();
    
    tone( pinSiren, freqSiren );

    switch( stateSiren )
    {
        case    INCREASING:
            freqSiren++;
            if( freqSiren == 800 )
                stateSiren = DECREASING;
        break;
        
        case    DECREASING:
            freqSiren--;
            if( freqSiren == 700 )
                stateSiren = INCREASING;
        break;
        
    }//switch
    
}//SirenStateMachine


void loop() 
{
    SirenStateMachine();
    LightsControl();
    
}//loop
  
void LightsControl( void )
{
    static unsigned long
        timeLights = 0,
        timeLightsDelay = 0;
    static bool
        state = false;
    static byte
        outer = 0,
        inner = 0;
    unsigned long
        timeNow;        
    timeNow = millis();
    if( (timeNow - timeLights) < timeLightsDelay )
        return;

    timeLights = timeNow;
    
    if( outer < 15 )
    {
        if( inner < 5 )
        {
            if( state == false )
            {
                state = true;
                DriveLights( 0b00001111 );
                timeLightsDelay = 50;   
                
            }//if
            else
            {
                state = false;
                DriveLights( 0b00000000 );
                inner++;
                if( inner < 5 )
                    timeLightsDelay = 50;
                else
                    timeLightsDelay = 100;                
            }//else
            
        }//if
        else if( inner < 10 )
        {
            if( state == false )
            {
                state = true;
                DriveLights( 0b11110000 );
                timeLightsDelay = 50;
            }
            else
            {
                state = false;
                DriveLights( 0b00000000 );
                inner++;
                outer++;
                if( inner < 10 )
                    timeLightsDelay = 50;
                else
                    timeLightsDelay = 100;
            }//else
            
        }//if

    }//if
    else if( outer < 65 )
    {
        if( state == false )
        {
            state = true;
            DriveLights( 0b00001111 );
            timeLightsDelay = 100;    
        }
        else
        {
            state = false;
            DriveLights( 0b11110000 );
            timeLightsDelay = 100;
            outer++;    
            
        }//else
                
    }//elseif
    else
    {
        //reset
        outer = 0;
        inner = 0;        
        timeLightsDelay = 0;
        
    }//else

}//LightsStateMachine

void DriveLights( byte pattern )
{
    byte
        mask = 0x01;
        
    for( int i=0; i<8; i++ )
    {
        digitalWrite( grpinLights[i], (pattern & mask)?HIGH:LOW );
        mask <<= 1;
        
    }//for
    
}//DriveLights

elvon_blunden:
Nowt wrong with that. Might not be a great practice for we humans to keep track of as we read the code, but the uC seems to have no trouble keeping track of the scope.

From quick test sketch both using "i":

Outer 0

Inner 0
  Inner 1
Outer 1
  Inner 0
  Inner 1
Outer 2
  Inner 0
  Inner 1

And this produces different results for same and different:

void setup() 
{
    int
        i, j, k;

    Serial.begin(9600);
    k = 0;
    for( i=0; i<5; i++ )
    {
        for( i=0; i<10; i++ )
        {
            k++;    
            
        }//for
        
    }//for

    Serial.print( "Inner and outer same: k is " ); Serial.println( k );

    k = 0;
    for( j=0; j<5; j++ )
    {
        for( i=0; i<10; i++ )
        {
            k++;    
            
        }//for
        
    }//for

    Serial.print( "Inner and outer different: k is " ); Serial.println( k );
    
}//setup

void loop()
{
}

Result:

Inner and outer same: k is 10
Inner and outer different: k is 50

In this case the processor never leaves the loop:

    k = 0;
    for( i=0; i<5; i++ )
    {
        for( i=0; i<2; i++ )
        {
            k++;    
            
        }//for
        
    }//for
.
.
.

This is bad practice that will more often than not produced unexpected results. It shouldn't be encouraged because of an anecdotal case where it did work by chance.

Blackfin:
an anecdotal case where it did work by chance.

The scopes are coded differently though. The one in this thread (and my test case, although I apologise for not posting my code, going off this thread's original) ints the variables in the fors, yours ints them in setup. I need more coffee but suspect that may be significant. Really cba to check further right now.

elvon_blunden:
The scopes are coded differently though. The one in this thread (and my test case, although I apologise for not posting my code, going off this thread's original) ints the variables in the fors, yours ints them in setup. I need more coffee but suspect that may be significant. Really cba to check further right now.

You're right: coded as for( int i=0; ...) things turn out okay. My mistake.

Still think it's a bad idea and bad form though :slight_smile:

Blackfin:
Still think it's a bad idea and bad form though :slight_smile:

Totally agree: asking for shit.