Help with startup button.

Hi, i’m tryng to finish this Art-Net ethernet project, the thing is i’d like to put a switch that you can press during startup to put the device in (static Ip mode), but if you’re not pushing it, should work with dynamic IP (DHCP). The thing is i dont know how to call this before setup :confused: :confused:

/****************************************************************
 * Art-Net to DMX Controller Interface                          *
 *                                                              *
 *                                                              *
 * Programmed for Arduino Nano with ENC28J60 Ethernet shield    *
 * Works with Arduino 1.6.8 using EtherCard & DmxSimple Library *
 * Takes ArtDMX packets sent via Ethernet from a PC and sends   *
 * DMX lighting values out to the DMX universe                  *
 *                                                              *
 * Based on ARTNET RECEIVER by Christoph Guillermet and E1.31   *
 * Receiver and pixel controller by Andrew Huxtable ported      *           
 * to use EtherCard library for ENC28J60 and Art-Net instead of *
 * E1.31 (sACN).                                                *
 *                                                              *
 * ENC26J60 module (not the Nano shield pins wired as follows:  *                                                              
 *                                                              *
 * Enc28j60 SO  -> Arduino pin 12 (MOSI)                        *
 * Enc28j60 SI  -> Arduino pin 11 (MISO)                        *
 * Enc28j60 SCK -> Arduino pin 13 (SCK)                         *
 * Enc28j60 CS  -> Arduino pin 10 (SS)                          *
 * Enc28j60 VCC -> Arduino 5V pin (3V3 pin didn't work for me)  *
 * Enc28j60 GND -> Arduino Gnd pin*                             * 
 *                                                              *
 ****************************************************************/

#include <EtherCard.h>
#include <DmxSimple.h>
#define bytes_to_short(h,l) ( ((h << 8) & 0xff00) | (l & 0x00FF) );
/* 
Set Ethernet interface ip address here. 
For direct link from PC Ethernet port to ENC board:

make ip 192.168.2.2 On PC, in ipv4 settings, make static IP 192.168.2.1, 
subnet mask 255.255.255.0 and gwip left empty
*/
#define staip
#ifdef staip
static byte myip[] = { 192,168,2,2 };   //set an IP address for the ENC device
#endif
//gateway ip address not needed
  //static byte gwip[] = { };
 
//ethernet mac address - //genetated from http://www.miniwebtool.com/mac-address-generator/ using Microchip OUI
//verified at http://sqa.fyicenter.com/Online_Test_Tools/MAC_Address_Format_Validator.php
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
 
byte Ethernet::buffer[580]; // tcp/ip send and receive buffer should be 512 dmx channels + Art-Net header + UDP header + Ethernet Header

const int number_of_channels=513; // 512 for one full DMX universe set to a low value to make the device work better
//byte buffer_dmx[number_of_channels+channel_position]; //buffer to store filetered DMX data    ***bypassed this bit to save SRAM***
const int art_net_header_size=17;
const int max_packet_size=172;    //
char ArtNetHead[8]="Art-Net";    //first byte of an ARTDMX packet contains "Art-Net"
short Opcode=0;

//function declarations
static void artnetPacket();
int checkARTDMX();
void sendDMX();
 

void setup()
{
  //Serial.begin(57600);        //for testing
  DmxSimple.usePin(3);          // DMX output is pin 3
  DmxSimple.maxChannel(512);    //should be 512
 
  ether.begin(sizeof(Ethernet::buffer), mymac, 10);   //10 at end initialises CS pin as pin 10 on arduino instead of default pin 8

// Static IP On
#ifdef staip
  ether.staticSetup(myip);
  #else
  ether.dhcpSetup();
 #endif
  
  
    
  // Register listener
  ether.udpServerListenOnPort(&artnetPacket, 6454);           //artnetPacket function to handle event, listen on port 6454 (default artnet port)
        //Serial.println( "register listener reached");       //print to serial for testing
}
 
void loop()
{
  // this must be called for ethercard functions to work.
  ether.packetLoop(ether.packetReceive());
  
}

// callback that deals with received packets 
static void artnetPacket(uint16_t port, uint8_t ip[4], uint16_t src_port, const char *data, uint16_t len) {
  //Serial.println( "artnetPacket reached");       //print to serial for testing
  //Make sure the packet is an ArtDMX packet
  int check = checkARTDMX(data, len);  
  if (check){     //if valid ArtDMX packet, send to DMX universe
    sendDMX(data);
  }
  //else if (!check){ //ArtPoll packet received, send reply
    //artpollReply();  
  //}
}

/*
Do some checks to see if it's an ArtNet packet. First 17 bytes are the ArtNet header, the rest is DMX values
Bytes 1 to 8 of an Art-Net Packet contain "Art-Net" so check for "Art-Net" in the first 8 bytes
Bytes 8 and 9 contain the OpCode - a 16 bit number that tells if its ArtPoll or ArtDMX
Don't worry about the rest of the bytes until byte 18 on (DMX channel levels) 
*/
int checkARTDMX(const char* messagein, int messagelength) {   
  //Serial.println( "checkARTDMX reached");       //print to serial for testing
  
  if(messagelength > art_net_header_size && messagelength <= max_packet_size) //check packet size first
  {
  //read header
     boolean match_artnet = 1;
     for (int i=0;i<7;i++)
     {
      if(messagein[i] != ArtNetHead[i])   //tests first byte for  "Art-Net"
    {
    match_artnet=0; break;      //if not an artnet packet we stop reading
    }   
     } 
     if (match_artnet==1)   //test flag
     {
    //check if ArtPoll or ArtDMX
    Opcode=bytes_to_short(messagein[9],messagein[8]);
     
     if(Opcode==0x5000)//5000 = ArtDmx
      {
       return 1;
      }   
       
     else if(Opcode==0x2000)//2000 = ArtPoll 
     {
     //An ArtPollReply should be sent in response supplying the controller with device info
     return 0;
     } 
   }
  }
  return -1;
}

/*
function to send the dmx data out using DmxSimple library function
Reads data directly from the packetBuffer and sends straight out
*/
void sendDMX(const char* packetBuffer) 
{
  //Serial.println( "sendDMX reached");       //print to serial for testing
  for(int i = 0; i < number_of_channels; i++)//channel position
  {
  //buffer_dmx[i]= byte(packetBuffer[i+17]);    //bypassed the dmx buffer altogether to save memory
  //DmxSimple.write(i,buffer_dmx[i]);
  DmxSimple.write(i,packetBuffer[i+17]);
  }
}
#ifdef staip
  ether.staticSetup(myip);
  #else
  ether.dhcpSetup();
 #endif

To do what you want you have to get rid of the compile time conditional code and go with a normal if statement. You can't do it "before" setup. There is no before setup. You can do it as the first thing in setup though. Be sure to give the user time to press the button unless they are to have it held down when they power on.

if(digitalRead(somePin) == LOW){   //if button is pressed
  ether.staticSetup(myip);
}else{
  ether.dhcpSetup();
}

Delta_G:

#ifdef staip

ether.staticSetup(myip);
 #else
 ether.dhcpSetup();
#endif




To do what you want you have to get rid of the compile time conditional code and go with a normal if statement. You can't do it "before" setup. There is no before setup. You can do it as the first thing in setup though. Be sure to give the user time to press the button unless they are to have it held down when they power on. 



if(digitalRead(somePin) == LOW){   //if button is pressed
 ether.staticSetup(myip);
}else{
 ether.dhcpSetup();
}

I try but is not working its like static byte myip = { 192,168,2,2 }; forced to put an static ip address. My idea is to put a switch so, the button will be all the time pressed or not.

Forget about it i just delete the static part and it worked like a charm. Thanks!

seekude:
I try but is not working its like static byte myip = { 192,168,2,2 }; forced to put an static ip address.

You mean the word static on that line? That has nothing to do with static or dynamic address. That word just means that the variable is only visible in this one file and can't be seen by any other file.

Yes you're right, its not working :confused:
I just forget to change my pc ip, so no matter what it forces the static ip.

What do you mean? I didn't say it wouldn't work. I just asked a question. You declined to answer.

Sorry, yes i deleted the word static, but i was using the swith without a resistor. I changed it:

pinMode(4,INPUT_PULLUP);
if(digitalRead(4) == LOW){ //if button is pressed
ether.staticSetup(myip);
}else{
ether.dhcpSetup();
}

Now it seems to be working.