Guest User

Untitled

a guest
Jun 15th, 2013
1,764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LCD.h>
  3. #include <LiquidCrystal_I2C.h>
  4. #include <SPI.h>
  5. #include <Ethernet.h>
  6. #include <ICMPPing.h>
  7. #include <Timer.h>
  8.  
  9. #define I2C_ADDR 0x3F // Define I2C Address where the SainSmart LCD is
  10. #define BACKLIGHT_PIN 3
  11. #define En_pin 2
  12. #define Rw_pin 1
  13. #define Rs_pin 0
  14. #define D4_pin 4
  15. #define D5_pin 5
  16. #define D6_pin 6
  17. #define D7_pin 7
  18.  
  19. byte mac[] = {you dun need to know this}; // mac address for ethernet shield (on the back of the shield)
  20. byte ip[] = {172, 16, 0, 16}; // ip address for ethernet shield
  21. byte pingAddr[6][4] = {{74, 125, 224, 198},
  22. {165, 254, 42, 40},
  23. {69, 53, 236, 17},
  24. {69, 13, 38, 151},
  25. {69, 13, 38, 153},
  26. {198, 12, 14, 165}}; // ip address to ping
  27. String pings[6];
  28.  
  29. SOCKET pingSocket = 0;
  30.  
  31. char buffer [256];
  32. ICMPPing ping(pingSocket);
  33. Timer t;
  34.  
  35. LiquidCrystal_I2C lcd = LiquidCrystal_I2C(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
  36. int panel = 0;
  37. int panels = 2;
  38.  
  39. boolean up_lastState;
  40. boolean up_state;
  41.  
  42. boolean down_lastState;
  43. boolean down_state;
  44.  
  45. String pingNames[] = {"Google", "Reddit", "Netflix",
  46. "CC Big 1", "CC Fish", "mc-sg.org"};
  47.  
  48. String titles [] = {"Ping Test (Major)",
  49. "Ping Test (Games)"};
  50.  
  51. void setup()
  52. {
  53. Serial.begin(9600);
  54. pinMode(6, INPUT);
  55. pinMode(7, INPUT);
  56. pinMode(8, OUTPUT);
  57.  
  58. Ethernet.begin(mac, ip);
  59. lcd.begin (20, 4);
  60.  
  61. // Switch on the backlight
  62. lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  63. lcd.setBacklight(HIGH);
  64.  
  65. // Position cursor and write some text
  66. updateLCD();
  67. t.every(6000, updateLCD);
  68. }
  69.  
  70. void loop()
  71. {
  72. checkButtons();
  73. t.update();
  74. }
  75.  
  76. void updateLCD()
  77. {
  78.  
  79. for (int j = 0; j < 3; j++)
  80. {
  81. pings[panel * 3 + j] = getPing(panel * 3 + j);
  82. }
  83.  
  84. lcd.clear();
  85. lcd.home();
  86.  
  87. lcd.print(titles[panel]);
  88. lcd.setCursor(18, 0);
  89. lcd.print("|");
  90.  
  91. for (int i = 0; i < 3; i++)
  92. {
  93. if (pingNames[i].length() < 10)
  94. {
  95. lcd.setCursor(0, i + 1);
  96. lcd.print(pingNames[panel * 3 + i]);
  97.  
  98. lcd.setCursor(9, i + 1);
  99. lcd.print("|");
  100.  
  101. Serial.println(pings[panel*3+i]);
  102. if (pings[panel * 3 + i] == "TimedOut")
  103. {
  104. lcd.setCursor(10, i + 1);
  105. lcd.print("TimedOut");
  106. }
  107. else
  108. {
  109. lcd.setCursor(11, i + 1);
  110. lcd.print(pings[panel * 3 + i]);
  111. }
  112. lcd.setCursor(18, i + 1);
  113. lcd.print("|");
  114. }
  115. else
  116. {
  117. Serial.println("ERROR: Server names need to be <10 characters");
  118. }
  119. }
  120. }
  121.  
  122. String getPing(int siteNum)
  123. {
  124. ping(2 , pingAddr[siteNum], buffer);
  125.  
  126. String sBuffer = String(buffer);
  127.  
  128. if (sBuffer == "Request Timed Out")
  129. {
  130. return "TimedOut";
  131. }
  132.  
  133. sBuffer = sBuffer.substring(sBuffer.indexOf("time=") + 5, sBuffer.indexOf("ms") + 2);
  134.  
  135. return sBuffer;
  136. }
  137.  
  138. void checkButtons()
  139. {
  140. // Up Button
  141. up_state = digitalRead(6);
  142.  
  143. if (up_state != up_lastState)
  144. {
  145. delay(50); // bounce protection
  146.  
  147. up_state = digitalRead(6);
  148. if (up_state == HIGH)
  149. {
  150. if (panel > 0)
  151. {
  152. panel--;
  153.  
  154. updateLCD();
  155.  
  156. tone(8, 450, 75);
  157. delay(75);
  158. tone(8, 550, 75);
  159. }
  160. }
  161. }
  162.  
  163. up_lastState = up_state;
  164.  
  165. // Down Button
  166. down_state = digitalRead(7);
  167.  
  168. if (down_state != down_lastState)
  169. {
  170. delay(50); // bounce protection
  171.  
  172. down_state = digitalRead(7);
  173. if (down_state == HIGH)
  174. {
  175. if (panel < panels - 1)
  176. {
  177. panel++;
  178. updateLCD();
  179.  
  180. tone(8, 850, 75);
  181. delay(75);
  182. tone(8, 950, 75);
  183. }
  184. }
  185. }
  186.  
  187. down_lastState = down_state;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment