Advertisement
ptnega

Untitled

May 18th, 2015
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <WiFi.h>
  3. #include <Servo.h>
  4.  
  5. char ssid[] = "hotel"; // name
  6. char pass[] = "tooristx";
  7. int PORT = 23;
  8.  
  9. bool F = 0, L = 0, R = 0;
  10.  
  11. // Sensor setup
  12. int echoPin = 2;
  13. int initPin = 3;
  14. long pulseTime = 0;
  15. long distance = 0;
  16.  
  17. Servo myServo;
  18. int servoPin = 9;
  19. int pos = 0;
  20.  
  21. // Motor setup
  22. int in[] = {0, 4, 5, 6, 7};
  23.  
  24. // Controling Setup
  25. void process(int t){
  26. switch (t){
  27. case 1:
  28. F = true;
  29. break;
  30. case 2:
  31. F = false;
  32. break;
  33. case 3:
  34. L = true;
  35. break;
  36. case 4:
  37. L = false;
  38. break;
  39. case 5:
  40. R = true;
  41. break;
  42. case 6:
  43. R = false;
  44. break;
  45. default:
  46. Serial.println("Command invalid");
  47. }
  48. }
  49.  
  50. int speed = 100;
  51.  
  52. void MoveFoward(int iMotor){
  53. if (iMotor == 1){
  54. digitalWrite(in[1],HIGH);
  55. analogWrite(in[2],255-speed);
  56. } else {
  57. digitalWrite(in[4],LOW);
  58. analogWrite(in[3],speed);
  59. }
  60. }
  61.  
  62. void MoveBackward(int iMotor){
  63. if (iMotor == 1){
  64. digitalWrite(in[1], LOW);
  65. analogWrite(in[2], speed);
  66. } else {
  67. digitalWrite(in[4], HIGH);
  68. analogWrite(in[3],255 - speed);
  69. }
  70. }
  71.  
  72. void MoveFoward(){
  73. MoveFoward(1);
  74. MoveFoward(2);
  75. }
  76.  
  77. void MoveBackward(){
  78. MoveBackward(1);
  79. MoveBackward(2);
  80. }
  81.  
  82. void TurnLeft(){
  83. MoveBackward(1);
  84. MoveFoward(2);
  85. }
  86.  
  87. void TurnRight(){
  88. MoveBackward(2);
  89. MoveFoward(1);
  90. }
  91. void StopMotor(int iMotor){
  92. if (iMotor == 1){
  93. digitalWrite(in[1],LOW);
  94. analogWrite(in[2],0);
  95. }
  96. else{
  97. digitalWrite(in[4],HIGH);
  98. analogWrite(in[3],255);
  99. }
  100. }
  101.  
  102. void StopMotors(){
  103. StopMotor(1);
  104. StopMotor(2);
  105. }
  106.  
  107. void DistanceCalculate(){
  108. digitalWrite(initPin,LOW);
  109. digitalWrite(initPin,HIGH);
  110. delayMicroseconds(10);
  111. digitalWrite(initPin,LOW);
  112. pulseTime = pulseIn(echoPin,HIGH);
  113. distance = pulseTime/58;
  114. delay(10);
  115. }
  116.  
  117. void ServoTurn(Servo svr, int pos){
  118. svr.write(pos);
  119. }
  120.  
  121. void ServoSweep(int delayTime){
  122. for (pos = 0; pos < 180; pos += 1){
  123. ServoTurn(myServo,pos);
  124. delay(delayTime);
  125. }
  126.  
  127. for (pos = 180; pos > 0; pos -= 1){
  128. ServoTurn(myServo,pos);
  129. delay(delayTime);
  130. }
  131. }
  132.  
  133. // Server Setup
  134.  
  135. int keyIndex = 0; // network key Index number (needed only for WEP)
  136. int status = WL_IDLE_STATUS;
  137. WiFiServer server(PORT);
  138. bool alreadyConnected = false; // whether or not the client was connected previously
  139.  
  140. void setup() {
  141. Serial.begin(9600);
  142.  
  143. myServo.attach(servoPin); // Servo attach
  144. // Sensor in-out
  145. pinMode(initPin,OUTPUT);
  146. pinMode(echoPin,INPUT);
  147.  
  148. for (int i = 1 ; i <= 4 ; i += 1) pinMode(in[i],OUTPUT); // Motor
  149.  
  150. //while (!Serial) {}
  151. if (WiFi.status() == WL_NO_SHIELD) {
  152. Serial.println("WiFi shield not present");
  153. while(true);
  154. }
  155.  
  156. String fv = WiFi.firmwareVersion();
  157. if (fv != "1.1.0") Serial.println("Firmware outdated");
  158.  
  159. // attempt to connect to Wifi network:
  160. while (status != WL_CONNECTED) {
  161. Serial.print("Attempting to connect to SSID: ");
  162. Serial.println(ssid);
  163. // Connect to WPA/WPA2 network
  164. status = WiFi.begin(ssid, pass);
  165.  
  166. delay(5000);
  167. }
  168. server.begin();
  169. printWifiStatus();
  170. }
  171.  
  172.  
  173. void loop() {
  174. WiFiClient client = server.available();
  175. DistanceCalculate();
  176. if (client) { // first byte
  177. if (!alreadyConnected) {
  178. client.flush();
  179. Serial.println("New client");
  180. client.println("Hi client!");
  181. alreadyConnected = true;
  182. }
  183. if (client.available() > 0) {
  184. // read incoming bytes from the client
  185. int thisChar = client.read();
  186. process(thisChar);
  187. // echo back to the client
  188. server.write(thisChar);
  189. Serial.println("Nhan cmnr");
  190. }
  191. if (L) TurnLeft();
  192. else if (R) TurnRight();
  193. if (F) MoveFoward();
  194.  
  195. if (!L && !R && !F) StopMotors();
  196. if (!(distance > 200 || distance < 2)){
  197. int x[] = {7, distance, pos};
  198. for (int i = 0; i < 3; ++i) server.write(x[i]);
  199. }
  200. }
  201. }
  202.  
  203.  
  204. void printWifiStatus() {
  205. Serial.print("SSID: ");
  206. Serial.println(WiFi.SSID());
  207.  
  208. IPAddress ip = WiFi.localIP();
  209. Serial.print("IP Address: ");
  210. Serial.println(ip);
  211.  
  212. long rssi = WiFi.RSSI();
  213. Serial.print("signal strength (RSSI):");
  214. Serial.print(rssi);
  215. Serial.println(" dBm");
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement