The entire code won't fit in a single message, so here it is in 2 parts:
Includes, Variables, Functions, and Setup:
I'm using the Arduino Ethernet, and v0022 of the compiler
/* Advanced Illumination
LEDLink
*/
#include <EEPROM.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#define maxLength 25
#include <memoryFree.h>
// these values are saved in EEPROM
const byte CH_EEPROM_ID = 0x99; // used to identify if valid data in EEPROM
byte mac[] = {
0x00, 0x22, 0xFE, 0x10, 0x00, 0x00 }; //update with factory MAC settings
byte default_ip[] = {
192, 168, 24, 253};
byte default_gate[] = {
192,168,24,1};
byte default_subnet[4];
String inString = String(maxLength);
int val;
byte r; //initial values to hold channel settings
byte g;
byte b;
//byte w;
char colorBuff[5];
Server server(80); //server port, HTML
const byte ch0Pin = 5; // channel0 output PWM pin
const byte ch1Pin = 6; // channel1 output PWM pin
const byte ch2Pin = 9; // channel2 output PWM pin
const byte strb0Pin = 4; // channel0 STRB enable pin
const byte strb1Pin = 7; // channel1 STRB enable pin
const byte strb2Pin = 8; // channel2 STRB enable pin
// TODO reserve pins 2 & 3 for hardware interrupts
const char swVer[]="0.02a - Alpha"; //ethernet software version number
const char fwVer[]="010-100"; //controller firmware version number
const char serialNum[]="000001"; //controller serial number
//constants used to identify EEPROM addresses
const int CH_ID_ADDR = 0; // pointer to the EEPROM address used to store the user channel settings
const int R_PIN_ADDR = 1; // the EEPROM address used to store the pin
const int G_PIN_ADDR = 2; // ""
const int B_PIN_ADDR = 3; // ""
const int CH_INTERVAL_ADDR = 4; // the EEPROM address used to store the interval
const int IP_ADDR_ID = 5; //pointer to the EEPROM address used to store the user IP ADDRESS
const int IP_ADDR[] = {
6,7,8,9};
//FUNCTIONS*********
//------------------------------setChannelPower--------------------------------------
void setChannelPower()
{
analogWrite(ch0Pin, r);
analogWrite(ch1Pin, g);
analogWrite(ch2Pin, b);
delay(5);
// analogWrite(ch3Pin, w); //remnant from 4 channels
// delay(5);
EEPROM.write(CH_ID_ADDR, 0x99);
EEPROM.write(R_PIN_ADDR, r);
EEPROM.write(G_PIN_ADDR, g);
EEPROM.write(B_PIN_ADDR, b);
delay(5);
}
//-----------------------------------------------------------------------------------
//------------------------------setMode----------------------------------------------
void setMode()
{
//TODO: add continuous and strobe modes
// User will switch between modes using software
// strobe mode will need to shut off the light and enable
//the high current sense resistor
}
//-----------------------------------------------------------------------------------
//------------------------------setFactoryDefault------------------------------------
void setFactoryDefault()
{
//TODO: add code to write all 0x00 to EEPROM
// When controller first boots, it will set default values when it sees that
// EEPROM_IDs are 00 and assign default variables accordingly
for(unsigned int i = 0; i < 512; i++)
{
EEPROM.write(i, 0x00);
}
}
//----------------------------------------------------------------------------------
//-----------------------------configureDHCP----------------------------------------
/* void configureDHCP()
{
// start the Ethernet connection:
if (Server.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
*/
//-----------------------------displayInfo------------------------------------------
void displayInfo()
{
Serial.print("\n<Channel Settings>");
Serial.println();
Serial.print("Channel 0: ");
Serial.println(r,DEC);
Serial.print("Channel 1: ");
Serial.println(g,DEC);
Serial.print("Channel 2: ");
Serial.println(b,DEC);
Serial.print("\n<TCP/IP settings>");
Serial.println();
Serial.print("IP Address: ");
for (byte i = 0; i <4; i++)
{
Serial.print(default_ip[i], DEC);
Serial.print(".");
}
Serial.println();
Serial.print("Gateway: ");
for (byte i = 0; i <4; i++)
{
Serial.print(default_gate[i], DEC);
Serial.print(".");
}
Serial.println();
Serial.print("Subnet: ");
for (byte i = 0; i <4; i++)
{
Serial.print(default_subnet[i], DEC);
Serial.print(".");
}
Serial.println();
Serial.print("\n<Hardware Information>");
Serial.println();
// Serial.println("Software Version: ");
// Serial.print(swVer);
Serial.print("Firmware Version: ");
Serial.println(fwVer);
Serial.print("Serial Number: ");
Serial.println(serialNum);
//TODO-- add more stuff to display here
}
//----------------------------------------------------------------------------------
//*******************************SETUP**********************************************
void setup()
{
Serial.begin(115200);
//read EEPROM and determine if channel data and setup data has been written
//
byte id = EEPROM.read(CH_ID_ADDR); // read the first byte from the EEPROM
if( id == CH_EEPROM_ID) //looks for 0x99 in ID_ADDR 0
{
// here if the id value read matches the value saved when writing eeprom
Serial.println("Using channel data from EEPROM");
r = EEPROM.read(R_PIN_ADDR);
g = EEPROM.read(G_PIN_ADDR);
b = EEPROM.read(B_PIN_ADDR);
delay(50);
analogWrite(ch0Pin, r); //write EEPROM channel data to output pins
analogWrite(ch1Pin, g);
analogWrite(ch2Pin, b);
}
else
{
// here if the ID is not found, write the default data
Serial.println("Writing default channel data to controller");
analogWrite (ch0Pin, 255); //default is 255 = 100% output
analogWrite (ch1Pin, 255);
analogWrite (ch2Pin, 255);
// analogWrite (ch3Pin, 255); //channel 4 remnant
}
Ethernet.begin(mac, default_ip);
server.begin();
displayInfo();
}