void loop() {
long currentMillis = millis(); //take the current runtime
// You MUST call the NmraDcc.process() method frequently from the Arduino loop() function for correct library operation
Dcc.process(); //run the nmra dcc library (decodes bus signal - I'm getting the set_port[] variable set
brightness[0] = Dcc.getCV(513); //read the brightness from the eeprom using the nmra dcc lib
brightness[1] = Dcc.getCV(514);
brightness[2] = Dcc.getCV(515);
brightness[3] = Dcc.getCV(516);
brightness[4] = Dcc.getCV(517);
brightness[5] = Dcc.getCV(518);
brightness[6] = Dcc.getCV(519);
brightness[7] = Dcc.getCV(520);
brightness[8] = Dcc.getCV(521);
brightness[9] = Dcc.getCV(522);
mode[0] = Dcc.getCV(523); // read the mode (0=on/off, 1=flicker fluorescent lamp damaged, 2=welding, 3=vapor lamp, 4=fluorescent lamp, 5 + time(max 2,5s)=blink, 11=nothing
mode[1] = Dcc.getCV(524);
mode[2] = Dcc.getCV(525);
mode[3] = Dcc.getCV(526);
mode[4] = Dcc.getCV(527);
mode[5] = Dcc.getCV(528);
mode[6] = Dcc.getCV(529);
mode[7] = Dcc.getCV(530);
mode[8] = Dcc.getCV(531);
mode[9] = Dcc.getCV(532);
if( FactoryDefaultCVIndex && Dcc.isSetCVReady())
{
FactoryDefaultCVIndex--; // Decrement first as initially it is the size of the array
Dcc.setCV( FactoryDefaultCVs[FactoryDefaultCVIndex].CV, FactoryDefaultCVs[FactoryDefaultCVIndex].Value);
}
for ( i = 0; i < 10; i++ ) { //walk through the ports
if ( set_port[i] == HIGH ) { // go on if defined port goes high
switch ( mode[i] ) { //decide what mode to use
case 0: // on/off
SoftPWMSet(port[i], brightness[i]);
break;
case 1: // flicker tube damaged, goes on/off randomly
random_value[0] = random(200, 1000); //first random value
random_value[1] = random(200, 1000); //second random value
if ( currentMillis > previousMillis[0] ) { //if time is come
SoftPWMSet(port[i], brightness[i]) ; // set to port the brightness on the led
previousMillis[0] = currentMillis + random_value[0]; //calculate the new time for running the timer
}
if ( currentMillis > previousMillis[1] ) {
SoftPWMSet(port[i], 0) ;
previousMillis[1] = currentMillis + random_value[1];
}
break;
case 2: // welding - you know? random flashes of one led
random_value[2] = random(50, 100);
random_value[3] = random(50, 100);
random_value[4] = random(500, 10000);
if ( currentMillis > previousMillis[2] && break_[0] == HIGH) {
SoftPWMSet(port[i], brightness[i]) ;
previousMillis[2] = currentMillis + random_value[2];
}
if ( currentMillis > previousMillis[3] ) {
SoftPWMSet(port[i], 0);
previousMillis[3] = currentMillis + random_value[3];
}
if ( currentMillis > previousMillis[4] ) {
break_[0] ^= 1;
previousMillis[4] = currentMillis + random_value[4];
}
break;
case 3: // vapor lamp, starts with a flash and then the brightness goes slowly to max brightness
if ( break_[1] == LOW ) { //be shure to run once (the flash at the beginning)
SoftPWMSet(port[i], 255); //set the led port to maximum
previousMillis[5] = currentMillis + 20; //set the new timer time
break_[1] = HIGH; //be shure this timer runs only once - I have problems with this how to start this case again with set_port[i] == true
break_[2] = LOW; //set the second timer
j = 0; // couter variable for fading
}
if ( currentMillis > previousMillis[5] && j < brightness[i] && break_[2] == LOW ) { //start the second timer if time is come and brightness isn't maximum
SoftPWMSet(port[i], j++ ); //increase the brightness
previousMillis[5] = currentMillis + 200; // set the next time the second timer should run
}
if ( j == brightness[i] ) { //stop the second timer, brightness maximum is reached
break_[2] = HIGH;
}
break;
case 4: // flourescent light, flashes once, then full light, like case 3 but simpler
if ( break_[3] == LOW ) {
SoftPWMSet(port[i], 255);
previousMillis[6] = currentMillis + 100;
break_[3] = HIGH;
break_[4] = LOW;
}
if ( currentMillis > previousMillis[6] && break_[4] == LOW) {
SoftPWMSet(port[i], 0 );
previousMillis[7] = currentMillis + 300;
break_[4] = HIGH;
break_[5] = LOW;
}
if ( currentMillis > previousMillis[7] && break_[5] == LOW) {
SoftPWMSet(port[i], brightness[i] );
break_[5] = HIGH;
}
break;
case 5: // 5 + time(max 2,5s)=blink
if ( currentMillis > previousMillis[8]) {
if ( last[0] == 0 ) {
last[0] = brightness[i];
SoftPWMSet(port[i], last[0] );
}
else {
last[0] = 0;
SoftPWMSet(port[i], last[0] );
}
previousMillis[8] = currentMillis + 500;
}
break;
case 6: // 5 + time(max 2,5s)=blink
if ( currentMillis > previousMillis[9]) {
if ( last[0] == 0 ) {
last[0] = brightness[i];
SoftPWMSet(port[i], last[0] );
}
else {
last[0] = 0;
SoftPWMSet(port[i], last[0] );
}
previousMillis[9] = currentMillis + 1000;
}
break;
case 7: // 5 + time(max 2,5s)=blink
if ( currentMillis > previousMillis[10]) {
if ( last[0] == 0 ) {
last[0] = brightness[i];
SoftPWMSet(port[i], last[0] );
}
else {
last[0] = 0;
SoftPWMSet(port[i], last[0] );
}
previousMillis[10] = currentMillis + 1500;
}
break;
case 8: // 5 + time(max 2,5s)=blink
if ( currentMillis > previousMillis[11]) {
if ( last[0] == 0 ) {
last[0] = brightness[i];
SoftPWMSet(port[i], last[0] );
}
else {
last[0] = 0;
SoftPWMSet(port[i], last[0] );
}
previousMillis[11] = currentMillis + 2000;
}
break;
case 9: // 5 + time(max 2,5s)=blink
if ( currentMillis > previousMillis[12]) {
if ( last[0] == 0 ) {
last[0] = brightness[i];
SoftPWMSet(port[i], last[0] );
}
else {
last[0] = 0;
SoftPWMSet(port[i], last[0] );
}
previousMillis[12] = currentMillis + 2500;
}
break;
}
}
else {
SoftPWMSet(port[i], 0);
}
old_port[i] = set_port[i];
}
if ( currentMillis > previousMillis[19] ) {
Serial.print("set_port[0] :");
Serial.println(set_port[0], DEC);
Serial.print("break_[1] :");
Serial.println(break_[1], DEC);
previousMillis[19] = currentMillis + 1500;
}
}
I've added comments, and hope you could help me to do this right!
Thanks.