Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

arduino: better timer overflow handling, should work forever ;o)

  • Loading branch information...
commit d452302fd30f1fd9e04bb9bdea61df75d1e97417 1 parent 193ee92
kiilo authored March 06, 2012
20  arduino_sketchbook/node_arduino/node_arduino.ino
@@ -16,6 +16,7 @@ node arduino
16 16
  by kiilo (Tobias Hoffmann)
17 17
  */
18 18
 
  19
+
19 20
 #include <SPI.h>
20 21
 #include <Ethernet.h>
21 22
 #include <TextFinder.h>
@@ -42,7 +43,7 @@ int PinStatus[] = {
42 43
 int AnalogEnable[] = {
43 44
   0, 0, 0, 0, 0, 0};
44 45
 int Analog;
45  
-int AnalogStatus[] = { 
  46
+int AnalogStatus[] = {
46 47
   -1, -1, -1, -1, -1, -1}; // it will update on first reading
47 48
 
48 49
 long PingTimer = 0;
@@ -65,6 +66,8 @@ void setup() {
65 66
   delay(1000);
66 67
   connectTCP();
67 68
   // if you get a connection, report back via serial:
  69
+  PingTimer = millis();
  70
+  UpdateTimer = millis();
68 71
 }
69 72
 
70 73
 void loop()
@@ -102,8 +105,8 @@ void loop()
102 105
   }
103 106
 
104 107
   // OUTGOING MESSAGES /////////////////////////////////////////////////
105  
-  if (UpdateTimer < millis()) {
106  
-    UpdateTimer = millis() + UpdateInterval;
  108
+  if ((long)(UpdateTimer - millis()) < 0) {
  109
+    UpdateTimer = (long)(UpdateTimer + UpdateInterval);
107 110
 
108 111
     for(Pin = 2; Pin < 10; Pin++) {
109 112
       if(PinModes[Pin] == 0) {
@@ -134,8 +137,8 @@ void loop()
134 137
     }
135 138
   }
136 139
 
137  
-  if (PingTimer < millis()) {
138  
-    PingTimer = millis() + 20000;
  140
+  if ((long)(PingTimer - millis()) < 0) {
  141
+    PingTimer = (long)(PingTimer + 20000);
139 142
     client.write("p ");
140 143
     client.print(millis());
141 144
     client.write("\n");
@@ -158,17 +161,16 @@ void loop()
158 161
 }
159 162
 
160 163
 void connectTCP() {
161  
-  Serial.println("connect");
  164
+  Serial.println("con");
162 165
   if (client.connect("kiilo.org", 7000)) {
163  
-    Serial.println("connected");
  166
+    //Serial.println("200");
164 167
     // send secret key fo auth:
165 168
     client.println("k BC37ACB390EF2");
166 169
     TextFinder finder( client, 1);
167 170
   }
168 171
   else {
169 172
     // kf you didn't get a connection to the server:
170  
-    Serial.println("connection failed");
  173
+    Serial.println("400");
171 174
   }
172 175
 }
173 176
 
174  
-

0 notes on commit d452302

Please sign in to comment.
Something went wrong with that request. Please try again.