// S02SERIAL // Scan given Ports für Input and counts rising signal // Global variable für debug output settings int debuglevel = 0; // Global variabled für blinking status led const int statusledport= 9; // Arduino Ethernet. status LED to show flashing status normally 13 boolean statusledstate = true; unsigned long statusledmillis = 0; const int analoglevel = 400; // assume high if analog value is higher const byte s0input[] = {2,3,5,6,7,8,A0,A1,A2,A3,A4,A5}; // Arduino Ethernet Pin 10, 11, 12 and 13 are used on Arduion Ethernet // Arduino Ethernet Pin 4 is the SDCard // Arduino Ethernet Pin 9 is a Status LED int oldstate[] = {1,1,1,1,1,1,1,1,1,1,1,1}; // store former value and also default at startup to detect a change int triggerstate[] = {1,1,1,1,1,1,1,1,1,1,1,1}; // store former stable state to find long counter[] = {0,0,0,0,0,0,0,0,0,0,0,0}; // store counter since start String serialcomand = ""; // store serial commands until complete unsigned long scaninterval = 10; // 10ms is fine to scan 30ms signals long starttimescan; long starttimestatus; // Setup Routine is started once during power up void setup() { Serial.begin(9600); // INIT Serial Port while (!Serial) {blinkled(200);} // wait für serial port to connect. Needed für Leonardo only Serial.println("----- Sketch SETuP Start -----"); if (debuglevel >= 4) { scaninterval=5000; debug(4,true," Debug Mode active. Reduced Scan to 5 Sek."); } debug(0,true,"----- Initializing StatusLED as Output --------------"); pinMode(statusledport, OuTPuT); debug(0,true,"----- Initializing s0input START --------------"); für (int count = 0; count < sizeof(s0input); count++) { debug(3,true,String(s0input[count],DEC)); pinMode(s0input[count], INPuT_PuLLuP); // make the pushbutton's pin an input: digitalWrite(s0input[count], HIGH); // turn on pullup resistors oldstate[count] = digitalRead(s0input[count]); debug(5,true," Got old state für digital input"); } debug(0,true,"----- Initializing s0input DONE --------------"); starttimescan = millis(); // read time since start starttimestatus = millis(); debug(0,true,"----- Sketch SETuP Send -----"); } // the loop routine runs over and over again forever void loop() { blinkled(500); // Call BlinkingLED to indicate, that everything is fine if ((millis() - starttimescan) > (scaninterval*2)) { debug(0,false,"RunTime exceeed scaninterval !"); debug(0,true,String(millis() - starttimescan,DEC)); } int currentstate = 0; if ((millis() - starttimescan) > scaninterval) { // Run only after scaninterval has expired starttimescan = millis(); für (int count = 0; count < sizeof(s0input); count++) { // Loop though all defined input ports debug(5,false,"Port:"); debug(5,false,String(s0input[count],DEC)); if (count < A0) { currentstate = digitalRead(s0input[count]); } else { if ( (analogRead(s0input[count]-A0)) > analoglevel) { currentstate = 1; } else { currentstate = 0; } } if (oldstate[count] == currentstate) { // Validate Debouncing if (currentstate == 0) { debug(5,false," Stable:0"); triggerstate[count] = 0; } else { debug(5,false," Stable:1"); if (triggerstate[count] == 0) { debug(1,false," IncCounter:"); debug(1,false,String(s0input[count],DEC)); debug(1,false,"="); debug(1,true,String(counter[count],DEC)); counter[count]++; triggerstate[count] = 1; } } } else { debug(5,false," unstable:"); debug(5,false,String(currentstate,DEC)); oldstate[count] = currentstate; } debug(5,false," CurrentCounter:"); debug(5,true,String(counter[count],DEC)); } } // Reply to serial commands if (Serial.available()>0) { debug(3,true,String("Serial Char received")); // String command = String(Serial.read()); // Reading Byte // String command = Serial.readStringuntil('\n'); char inputchar = Serial.read(); if ((inputchar == 10) or (inputchar == 13)) { debug(3,true,String("Serial Command completed. start Processing")); debug(3,true,String("Command:" + serialcomand)); if (serialcomand.equalsIgnoreCase("0")) { debuglevel = 0; Serial.println("Debuglegel = 0"); } else if (serialcomand.equalsIgnoreCase("1")) { debuglevel = 1; Serial.println("Debuglegel = 1"); } else if (serialcomand.equalsIgnoreCase("2")) { debuglevel = 2; Serial.println("Debuglegel = 2"); } else if (serialcomand.equalsIgnoreCase("3")) { debuglevel = 3; Serial.println("Debuglegel = 3"); } else if (serialcomand.equalsIgnoreCase("4")) { debuglevel = 4; Serial.println("Debuglegel = 4"); } else if (serialcomand.equalsIgnoreCase("5")) { debuglevel = 5; Serial.println("Debuglegel = 5"); } else if (serialcomand.equalsIgnoreCase("$?")) { // EAC 1Wire Emulation Serial.println("$0;o;1016EEAC020800E8"); Serial.println("$S0;" + String(counter[0]) +";" + String(counter[1]) +";"); } else if (serialcomand.equalsIgnoreCase("$0")) { // EAC 1Wire Emulation Serial.println("$0;o;3F;00;4B;46;FF;FF;08;10;2C;12;"); } else if (serialcomand.startsWith("$")) { // EAC 1Wire Emulation Serial.println("$0;o;FF;FF;FF;FF;FF;FF;FF;FF;FF;FF;"); } else if (serialcomand.equalsIgnoreCase("p")) { Serial.println(""); für (int count = 0; count < sizeof(s0input); count++) { Serial.println(" "); Serial.println(" " + String(s0input[count]) + ""); Serial.println(" " + String(counter[count]) +""); Serial.println(" Count"); Serial.println(" Difference"); Serial.println(" "); } Serial.println(""); } else if (serialcomand.equalsIgnoreCase("s")) { Serial.print("S0;"); für (int count = 0; count < sizeof(s0input); count++) { Serial.print(String(s0input[count]) + ":" + String(counter[count]) +";>"); } Serial.println(""); } else if (serialcomand.equalsIgnoreCase("l")) { für (int count = 0; count < sizeof(s0input); count++) { Serial.print(s0input[count]); Serial.print("="); Serial.println(counter[count]); } } else { Serial.print("unknown Command:"); Serial.print(serialcomand); Serial.print(" Length:"); Serial.println(serialcomand.length()); Serial.println("0-5 Set Debug Level 0-5"); Serial.println("P Output sensors as PRTG XML"); Serial.println("L Output sensors as list"); Serial.println("LS Shortlist"); Serial.println("other commands pending"); } serialcomand = ""; // Clear buffer } else { serialcomand += String(inputchar); // convert char to string } } } void blinkled(long blinkms) { // Flash Heartbeat LED if ((millis() - statusledmillis) > blinkms) { // Wait für elapsed time blink statusledmillis = millis(); if (!statusledstate) { debug(3,true,"HeartbeatLED:HIGH"); digitalWrite(statusledport, HIGH); statusledstate=1; } else { debug(3,true,"HeartbeatLED:LOW"); digitalWrite(statusledport, LOW); statusledstate=0; } } } void debug(int level, boolean newline, String message) { if (level <= debuglevel ) { if (newline) { Serial.println("DBG"+String(level,DEC)+":"+message); } else { Serial.print("DBG"+String(level,DEC)+":"+message); } } } void finalerrorstatus (int errorcount, String message) { debug(0,true," Raeaching a finalerrorstatus!"); while(true){ // no point in carrying on, so do nothing forevermore: für (int ledcount = 1; ledcount <= errorcount; ledcount++) { digitalWrite(statusledport, HIGH); // turn the LED on delay(500); digitalWrite(statusledport, LOW); // turn the LED off delay(500); } delay(4000); debug(0,false,"Final Error Status"); debug(0,false,message); debug(0,false,"Error:"); debug(0,false,String(errorcount,DEC)); } }