Page 3 of 32 FirstFirst 1234513 ... LastLast
Results 21 to 30 of 312

Thread: Simple code for Arduino Uno/Mega. Up to 48 realy/SSR channels, with Random mode

  1. #21
    Join Date
    Sep 2014
    Posts
    20
    Post Thanks / Like

    Default Re: Simple code for Arduino Uno/Mega. Up to 48 realy/SSR channels, with Random mode

    Ok, so I have been following this thread and I would like to know what is the final complete code to run a mega 2560 with all possible output pins enabled? No pwm. Just on off.Also, why in the codes posted above are there so many indentations?

  2. #22
    Join Date
    Nov 2012
    Posts
    7
    Post Thanks / Like

    Default Re: Simple code for Arduino Uno/Mega. Up to 48 realy/SSR channels, with Random mode

    I have been trying different sketches with no luck. I had a good sketch and a sequence that I was at the end of the song with my sequence when my computer got a virus and I can not figure it out so I'm having to start from scratch again. I like the random on this sketch but I cant get it to work with out having error codes, so any help would be greatly appreciated I am using a mega and am going to attempt vixen 3 I used vixen 2 on my last sequence only because I couldn't get 3 to work properly.

  3. #23
    Join Date
    Nov 2014
    Location
    Destin, Fl
    Posts
    157
    Post Thanks / Like

    Default Re: Simple code for Arduino Uno/Mega. Up to 48 realy/SSR channels, with Random mode

    Quote Originally Posted by Eman212 View Post
    Ok, so I have been following this thread and I would like to know what is the final complete code to run a mega 2560 with all possible output pins enabled? No pwm. Just on off.Also, why in the codes posted above are there so many indentations?
    The reason why there are so many indentations, is that there are nested if/else statements.

    This is what I'm using and be SURE to read lines 6-8:
    Code:
    // Copy from this line down to your new sketch
    // This code was written by Victor Perez for doityourselfchristmas.com based on the code from Zparticle, Si_champion and Neil Tapp.
    // To adapt the code to your case, just change this top section, with the #define lines.
    // how many channels will vixen be sending. Can be set to any number from 1 to 48 for Arduino Mega, and 1 to 18 for Arduino Uno.
    
    // You need to increase your channel count by 1 and the last channel is used to force all lights on. If the last channel is 0 
    // (which is what you get from Vixen when nothing is running) all lights come on. In your sequence set this channel to anything 
    // other than 0 for the entire sequence. 
    
    #define CHANNEL_COUNT 24
    
    // speed for the com port for talking with vixen. From 9600 to 115200. Use the same speed as set in Vixen.
    #define VIXEN_COM_SPEED 57600
    
    // Timeout waiting for serial input before going to random mode (in milliseconds).
    #define TIME_OUT 20000
    
    // If the relays turn On and Off opposite to Vixen sequence, change "#define MODE NOT_INVERTED" for "#define MODE INVERTED"
    #define NOT_INVERTED 0
    #define INVERTED 1
    #define MODE NOT_INVERTED
    
    // which pins control which channels
    // You can change these assignment to use different pins, but be very careful to not repeat the same pin number for 2 channels. 
    // which pins control which channels
    #define CHANNEL01 2
    #define CHANNEL02 3
    #define CHANNEL03 4
    #define CHANNEL04 5
    #define CHANNEL05 6
    #define CHANNEL06 7
    #define CHANNEL07 8
    #define CHANNEL08 9
    #define CHANNEL09 45
    #define CHANNEL10 44
    #define CHANNEL11 43
    #define CHANNEL12 42
    #define CHANNEL13 41
    #define CHANNEL14 40
    #define CHANNEL15 39
    #define CHANNEL16 38
    #define CHANNEL17 29
    #define CHANNEL18 28
    #define CHANNEL19 27
    #define CHANNEL20 26
    #define CHANNEL21 25
    #define CHANNEL22 24
    #define CHANNEL23 23
    #define CHANNEL24 22
    
    int channels[] = { CHANNEL01, CHANNEL02, CHANNEL03, CHANNEL04, CHANNEL05,
    		CHANNEL06, CHANNEL07, CHANNEL08, CHANNEL09,
    		CHANNEL10, CHANNEL11, CHANNEL12, CHANNEL13, CHANNEL14, CHANNEL15,
    		CHANNEL16, CHANNEL17, CHANNEL18, CHANNEL19, CHANNEL20,
    		CHANNEL21, CHANNEL22, CHANNEL23, CHANNEL24 };
    
    int incomingByte[CHANNEL_COUNT];
    
    int i = 0;     // Loop counter
    
    volatile unsigned long timer_a = 0; // new line
    
    //setup the pins/ inputs & outputs
    void setup() {
    
    	// specifically for the UNO
    	//sei();  
    
    // initalize PWM Channels / Pins
    	for (i = 0; i < CHANNEL_COUNT; i++) {
    		pinMode(channels[i], OUTPUT);
    	}
    
    // set all the relays to off to start with
    	if (MODE == NOT_INVERTED) {
    		for (i = 0; i < CHANNEL_COUNT; i++) {
    			digitalWrite(channels[i], LOW);
    
    		}
    	}
    
    	else {
    		for (i = 0; i < CHANNEL_COUNT; i++) {
    			digitalWrite(channels[i], HIGH);
    		}
    	}
    
    	testSequence();
    
    // set up Serial according to the speed defined above.
    	Serial.begin(VIXEN_COM_SPEED);
    }
    
    void loop() 
    {
    	if (Serial.available() >= (CHANNEL_COUNT + 2)) 
    	{
    		timer_a = millis(); // new line
    		int uno = Serial.read();
    		if (uno == 126) 
    		{
    			int dos = Serial.read();
    			if (dos == 33) 
    			{
    				for (i = 0; i < CHANNEL_COUNT; i++) 
    				{				
    					incomingByte[i] = Serial.read();
    				}
    				if (incomingByte[CHANNEL_COUNT-1] == 0)
    				{
    				    turnLightsOn();
    				}
    				else
    				{
    					for (i = 0; i < CHANNEL_COUNT-1; i++)
    					{
    						if (incomingByte[i] < 127)
    						{
    							digitalWrite(channels[i], (MODE == NOT_INVERTED) ? LOW : HIGH);
    						}
    						else
    						{
    							digitalWrite(channels[i], (MODE == NOT_INVERTED) ? HIGH : LOW);
    						}
    					}
    				}
    			}
    		}
    	}
    // Random mode code. Random mode starts if no serial input has been received in TIME_OUT millisenconds
    	else {
      //turn them all on
            unsigned long diff = millis() - timer_a;
            if (diff >= TIME_OUT) {
             timer_a = millis ();
             turnLightsOn();
    	}
    }
    }
    
    void testSequence() {
    
    	if (MODE == NOT_INVERTED) {
    		for (i = 0; i < CHANNEL_COUNT; i++) {
    			digitalWrite(channels[i], HIGH);
    			delay(120);
    			digitalWrite(channels[i], LOW);
    			
    		}
    	}
    
    	else {
    		for (i = 0; i < CHANNEL_COUNT; i++) {
    			digitalWrite(channels[i], LOW);
    			delay(500);
    			digitalWrite(channels[i], HIGH);
    		}
    	}
    }
    
    void turnLightsOn()
    {
      for (i = 0; i < CHANNEL_COUNT; i++) {
      digitalWrite(channels[i], HIGH);
      }
    }

  4. #24
    Join Date
    Nov 2014
    Location
    Destin, Fl
    Posts
    157
    Post Thanks / Like

    Default Re: Simple code for Arduino Uno/Mega. Up to 48 realy/SSR channels, with Random mode

    Quote Originally Posted by Michance010 View Post
    I have been trying different sketches with no luck. I had a good sketch and a sequence that I was at the end of the song with my sequence when my computer got a virus and I can not figure it out so I'm having to start from scratch again. I like the random on this sketch but I cant get it to work with out having error codes, so any help would be greatly appreciated I am using a mega and am going to attempt vixen 3 I used vixen 2 on my last sequence only because I couldn't get 3 to work properly.
    Post the code you're using!

  5. #25
    Join Date
    Nov 2012
    Posts
    7
    Post Thanks / Like

    Default Re: Simple code for Arduino Uno/Mega. Up to 48 realy/SSR channels, with Random mode

    I had a buddy stop by last night and got my old desktop working again so im in the process of transferring all my files to my new laptop, but im going to try and play around with it and add the random to it. I've got some work to do with it because when I tried playing my sequence my test leds didn't light up. I am happy I was able to save the hours of work I put into my first sequence I've ever done.
    This is the code that was working sorry don't know how to put it in a box



    /*
    46 CHANNEL LIGHT CONTROL USING ARDUINO MEGA

    46 MECHANICAL RELAY BOARD ON PINS 1-46
    */
    #define CHANNELS_COUNT 46

    #define Ch1 2 // PWM Pin 2
    #define Ch2 3 // PWM Pin 3
    #define Ch3 4 // PWM Pin 4
    #define Ch4 5 // PWM Pin 5
    #define Ch5 6 // PWM Pin 6
    #define Ch6 7 // PWM Pin 7
    #define Ch7 8 // PWM Pin 8
    #define Ch8 9 // PWM Pin 9
    #define Ch9 10 // PWM Pin 10
    #define Ch10 11 // PWM Pin 11
    #define Ch11 12 // PWM Pin 12
    #define Ch12 13 // PWM Pin 13
    /////////// digital pin - connects to relay board
    #define Ch13 14 // DIGITAL Pin 14
    #define Ch14 15 // DIGITAL Pin 15
    #define Ch15 16 // DIGITAL Pin 16
    #define Ch16 17 // DIGITAL Pin 17
    #define Ch17 18 // DIGITAL Pin 18
    #define Ch18 19 // DIGITAL Pin 19
    #define Ch19 20 // DIGITAL Pin 20
    #define Ch20 21 // DIGITAL Pin 21
    #define Ch21 22 // DIGITAL Pin 22
    #define Ch22 23 // DIGITAL Pin 23
    #define Ch23 24 // DIGITAL Pin 24
    #define Ch24 25 // DIGITAL Pin 25
    #define Ch25 26 // DIGITAL Pin 26
    #define Ch26 27 // DIGITAL Pin 27
    #define Ch27 28 // DIGITAL Pin 28
    #define Ch28 29 // DIGITAL Pin 29
    #define Ch29 30 // DIGITAL Pin 30
    #define Ch30 31 // DIGITAL Pin 31
    #define Ch31 32 // DIGITAL Pin 32
    #define Ch32 33 // DIGITAL Pin 33
    #define Ch33 34 // DIGITAL Pin 34
    #define Ch34 35 // DIGITAL Pin 35
    #define Ch35 36 // DIGITAL Pin 36
    #define Ch36 37 // DIGITAL Pin 37
    #define Ch37 38 // DIGITAL Pin 38
    #define Ch38 39 // DIGITAL Pin 39
    #define Ch39 40 // DIGITAL Pin 40
    #define Ch40 41 // DIGITAL Pin 41
    #define Ch41 42 // DIGITAL Pin 36
    #define Ch42 43 // DIGITAL Pin 37
    #define Ch43 44 // DIGITAL Pin 38
    #define Ch44 45 // DIGITAL Pin 39
    #define Ch45 46 // DIGITAL Pin 40
    #define Ch46 47 // DIGITAL Pin 41
    int Ch[CHANNELS_COUNT] = {Ch1, Ch2, Ch3, Ch4, Ch5, Ch6, Ch7, Ch8, Ch9, Ch10, Ch11, Ch12, Ch13, Ch14, Ch15, Ch16, Ch17, Ch18, Ch19, Ch20, Ch21, Ch22, Ch23, Ch24, Ch25, Ch26, Ch27, Ch28, Ch29, Ch30, Ch31, Ch32, Ch33, Ch34, Ch35, Ch36, Ch37, Ch38, Ch39, Ch40, Ch41,Ch42, Ch43, Ch44, Ch45, Ch46};
    int incomingByte[CHANNELS_COUNT]; // array to store the values from serial port
    void setup()
    {
    Serial.begin(57600); // set up Serial at 57600 bps
    for (byte i=0; i<CHANNELS_COUNT; i++) pinMode(Ch[i], OUTPUT); // declare channel pin as an output

    }
    void loop() {
    if (Serial.available() >= CHANNELS_COUNT) {
    for (int i=0; i<CHANNELS_COUNT; i++) {
    incomingByte[i] = Serial.read(); // read each byte
    if (i<45) { //arduino mega consists of 12 PWM
    analogWrite(Ch[i], incomingByte[i]); // Write current values to LED pins
    } else {
    digitalWrite (Ch[i], ~incomingByte[i]); // Write the Digital Output to the LED pin.
    }
    }
    }
    }

  6. #26
    Join Date
    Nov 2009
    Location
    Morrisburg, On, Canada
    Posts
    2,541
    Post Thanks / Like

    Default Re: Simple code for Arduino Uno/Mega. Up to 48 realy/SSR channels, with Random mode

    Quote Originally Posted by Michance010 View Post
    ...This is the code that was working sorry don't know how to put it in a box ...
    Use the code tags, like so;
    [ c o d e ]
    your code here
    [ / c o d e ]

    Remove the spaces, though. I put them in so it would show up.

  7. #27
    Join Date
    Nov 2014
    Location
    Austin, TX
    Posts
    164
    Post Thanks / Like

    Default Re: Simple code for Arduino Uno/Mega. Up to 48 realy/SSR channels, with Random mode

    Quote Originally Posted by Eman212 View Post
    Ok, so I have been following this thread and I would like to know what is the final complete code to run a mega 2560 with all possible output pins enabled? No pwm. Just on off.Also, why in the codes posted above are there so many indentations?
    The code in the first page works fine for up to 40 something channels, could be modified for even more if needed.
    It has a random mode that activates if you close Vixen.

    The indentations are there because they make code easier to read. Most programming tools add the indentations automatically, the Arduino IDE adds them automatically when you are writing any kind of loop.

  8. #28
    Join Date
    Nov 2014
    Posts
    1
    Post Thanks / Like

    Default Re: Simple code for Arduino Uno/Mega. Up to 48 realy/SSR channels, with Random mode

    Hi there,

    I uploaded your code to my arduino mega, and everything seems to work until I start my show... after that some of my channels are doing very random things. I have the serial port set up for 57600, but some of my channels arent blinking when they should be, or are blinking when they shouldnt be. Ive tried both inverted and non inverted mode, any suggestions on what else I should look at? any help is appreciated, thanks!

  9. #29
    Join Date
    Nov 2014
    Location
    Destin, Fl
    Posts
    157
    Post Thanks / Like

    Default Re: Simple code for Arduino Uno/Mega. Up to 48 realy/SSR channels, with Random mode

    Quote Originally Posted by jrwagner89 View Post
    Hi there,

    I uploaded your code to my arduino mega, and everything seems to work until I start my show... after that some of my channels are doing very random things. I have the serial port set up for 57600, but some of my channels arent blinking when they should be, or are blinking when they shouldnt be. Ive tried both inverted and non inverted mode, any suggestions on what else I should look at? any help is appreciated, thanks!
    Are both your sketch and Vixen set to 57600 as well?

  10. #30
    Join Date
    Nov 2014
    Location
    Destin, Fl
    Posts
    157
    Post Thanks / Like

    Default Re: Simple code for Arduino Uno/Mega. Up to 48 realy/SSR channels, with Random mode

    Quote Originally Posted by Michance010 View Post
    I had a buddy stop by last night and got my old desktop working again so im in the process of transferring all my files to my new laptop, but im going to try and play around with it and add the random to it. I've got some work to do with it because when I tried playing my sequence my test leds didn't light up. I am happy I was able to save the hours of work I put into my first sequence I've ever done.
    Is that working for you? If not, try this:
    Code:
    // Copy from this line down to your new sketch
     // This code was written by Victor Perez for doityourselfchristmas.com based on the code from Zparticle, Si_champion and Neil Tapp.
     // To adapt the code to your case, just change this top section, with the #define lines.
     // how many channels will vixen be sending. Can be set to any number from 1 to 48 for Arduino Mega, and 1 to 18 for Arduino Uno.
     
     #define CHANNEL_COUNT 46
    
     // speed for the com port for talking with vixen. From 9600 to 115200. Use the same speed as set in Vixen.
     #define VIXEN_COM_SPEED 57600
     
     // Timeout waiting for serial input before going to random mode (in milliseconds).
     #define TIME_OUT 2000
     
     // If the relays turn On and Off opposite to Vixen sequence, change "#define MODE NOT_INVERTED" for "#define MODE INVERTED"
     #define NOT_INVERTED 0
     #define INVERTED 1
     #define MODE NOT_INVERTED
    
     // which pins control which channels
     // You can change these assignment to use different pins, but be very careful to not repeat the same pin number for 2 channels. 
    #define CH01 2 // PWM Pin 2
    #define CH02 3 // PWM Pin 3
    #define CH03 4 // PWM Pin 4
    #define CH04 5 // PWM Pin 5
    #define CH05 6 // PWM Pin 6
    #define CH06 7 // PWM Pin 7
    #define CH07 8 // PWM Pin 8
    #define CH08 9 // PWM Pin 9
    #define CH09 10 // PWM Pin 10
    #define CH10 11 // PWM Pin 11
    #define CH11 12 // PWM Pin 12
    #define CH12 13 // PWM Pin 13
    // digital pin - connects to relay board
    #define CH13 14 // DIGITAL Pin 14
    #define CH14 15 // DIGITAL Pin 15
    #define CH15 16 // DIGITAL Pin 16
    #define CH16 17 // DIGITAL Pin 17
    #define CH17 18 // DIGITAL Pin 18
    #define CH18 19 // DIGITAL Pin 19
    #define CH19 20 // DIGITAL Pin 20
    #define CH20 21 // DIGITAL Pin 21
    #define CH21 22 // DIGITAL Pin 22
    #define CH22 23 // DIGITAL Pin 23
    #define CH23 24 // DIGITAL Pin 24
    #define CH24 25 // DIGITAL Pin 25
    #define CH25 26 // DIGITAL Pin 26
    #define CH26 27 // DIGITAL Pin 27
    #define CH27 28 // DIGITAL Pin 28
    #define CH28 29 // DIGITAL Pin 29
    #define CH29 30 // DIGITAL Pin 30
    #define CH30 31 // DIGITAL Pin 31
    #define CH31 32 // DIGITAL Pin 32
    #define CH32 33 // DIGITAL Pin 33
    #define CH33 34 // DIGITAL Pin 34
    #define CH34 35 // DIGITAL Pin 35
    #define CH35 36 // DIGITAL Pin 36
    #define CH36 37 // DIGITAL Pin 37
    #define CH37 38 // DIGITAL Pin 38
    #define CH38 39 // DIGITAL Pin 39
    #define CH39 40 // DIGITAL Pin 40
    #define CH40 41 // DIGITAL Pin 41
    #define CH41 42 // DIGITAL Pin 36
    #define CH42 43 // DIGITAL Pin 37
    #define CH43 44 // DIGITAL Pin 38
    #define CH44 45 // DIGITAL Pin 39
    #define CH45 46 // DIGITAL Pin 40
    #define CH46 47 // DIGITAL Pin 41
     
     int channels[] = {CH01,CH02,CH03,CH04,CH05 ,CH06,CH07,CH08,CH09,
     CH10,CH11,CH12,CH13,CH14,CH15,CH16,CH17,CH18,CH19,CH20,CH21,CH22,
     CH23,CH24,CH25,CH26,CH27,CH28,CH29,CH30,CH31,CH32,CH33,CH34,CH35,
     CH36,CH37,CH38,CH39,CH40,CH41,CH42,CH43,CH44,CH45,CH46};
    
     int incomingByte[CHANNEL_COUNT];
    
    int i = 0;     // Loop counter
    volatile unsigned long  timer_a = 0; // new line
    
    //setup the pins/ inputs & outputs
    void setup(){
    
      // specifically for the UNO
      sei();  
    
    // initalize PWM Channels / Pins
     for (i=0; i < CHANNEL_COUNT; i++){
        pinMode(channels[i], OUTPUT);
      }
    
    // set all the relays to off to start with
    if (MODE == NOT_INVERTED) {
     for (i=0; i < CHANNEL_COUNT; i++){
         digitalWrite(channels[i], LOW);
    
     }
    }
    
    else {
     for (i=0; i < CHANNEL_COUNT; i++){
         digitalWrite(channels[i], HIGH);
     }
    }
    
     testSequence();
     
    // set up Serial according to the speed defined above.
      Serial.begin(VIXEN_COM_SPEED);
    }
    
    void loop()
    {
       if (Serial.available() >= (CHANNEL_COUNT+2)) {
       timer_a = millis (); // new line
      
             for (i=0; i < CHANNEL_COUNT; i++) {
                 // read each byte
              incomingByte[i] = Serial.read();
             }
    		 if (MODE == NOT_INVERTED) {
    			for (i=0; i < CHANNEL_COUNT; i++){
    			int value = incomingByte[i];
    			if (value <= 127) {
    				digitalWrite(channels[i], LOW);
    			}
    			else {
    				digitalWrite(channels[i], HIGH);
    			}
    			}
    		 }
    		 else {
    		 for (i=0; i < CHANNEL_COUNT; i++){
    			int value = incomingByte[i];
    			if (value <= 127) {
    				digitalWrite(channels[i], HIGH);
    			}
    			else {
    				digitalWrite(channels[i], LOW);
    			}
    			}
    		 }
       }
    // Random mode code. Random mode starts if no serial input has been received in TIME_OUT millisenconds
       else {
         unsigned long diff = millis() - timer_a;
         if (diff >= TIME_OUT) {
           timer_a = millis ();
           int random_a = 0;
           for (i=0; i < CHANNEL_COUNT; i++){
             random_a = random(0, 2);
             if (random_a == 0) {
               digitalWrite(channels[i], LOW);
             }
             else {
               digitalWrite(channels[i], HIGH);
             }
           }
         }
       }
    }
    
    void testSequence(){
    
    if (MODE == NOT_INVERTED) {
     for (i=0; i < CHANNEL_COUNT; i++){
         digitalWrite(channels[i], HIGH);
         delay (500);
         digitalWrite(channels[i], LOW);
     }
    }
    
    else {
     for (i=0; i < CHANNEL_COUNT; i++){
         digitalWrite(channels[i], LOW);
         delay (500);
         digitalWrite(channels[i], HIGH);
       }
     }
    }

Page 3 of 32 FirstFirst 1234513 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •