Advertisement
Guest User

Test

a guest
May 18th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 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 DistanceCalculate(){
  88.     digitalWrite(initPin,LOW);
  89.     digitalWrite(initPin,HIGH);
  90.     delayMicroseconds(10);
  91.     digitalWrite(initPin,LOW);
  92.     pulseTime = pulseIn(echoPin,HIGH);
  93.     distance = pulseTime/58;
  94.     delay(10);
  95. }
  96.  
  97. void ServoTurn(Servo svr, int pos){
  98.     svr.write(pos);
  99. }
  100.  
  101. void ServoSweep(int delayTime){
  102.     for (pos = 0; pos < 180; pos += 1){
  103.         ServoTurn(myServo,pos);
  104.         delay(delayTime);
  105.     }
  106.  
  107.     for (pos = 180; pos > 0; pos -= 1){
  108.         ServoTurn(myServo,pos);
  109.         delay(delayTime);
  110.     }
  111. }
  112.  
  113. // Server Setup
  114.  
  115. int keyIndex = 0;                        // network key Index number (needed only for WEP)
  116. int status = WL_IDLE_STATUS;
  117. WiFiServer server(PORT);
  118. bool alreadyConnected = false; // whether or not the client was connected previously
  119.  
  120. void setup() {
  121.     Serial.begin(9600);
  122.  
  123.     myServo.attach(servoPin); // Servo attach
  124.     // Sensor in-out
  125.     pinMode(initPin,OUTPUT);
  126.     pinMode(echoPin,INPUT);
  127.  
  128.     for (int i = 1 ; i <= 4 ; i += 1) pinMode(in[i],OUTPUT); // Motor
  129.  
  130.     while (!Serial) {
  131.     }
  132.     if (WiFi.status() == WL_NO_SHIELD) {
  133.         Serial.println("WiFi shield not present");
  134.         while(true);
  135.     }
  136.  
  137.     String fv = WiFi.firmwareVersion();
  138.     if (fv != "1.1.0") Serial.println("Firmware outdated");
  139.  
  140.     // attempt to connect to Wifi network:
  141.     while (status != WL_CONNECTED) {
  142.         Serial.print("Attempting to connect to SSID: ");
  143.         Serial.println(ssid);
  144.         // Connect to WPA/WPA2 network
  145.         status = WiFi.begin(ssid, pass);
  146.  
  147.         delay(5000);
  148.     }
  149.     server.begin();
  150.     printWifiStatus();
  151.  }
  152.  
  153.  
  154. void loop() {
  155.     WiFiClient client = server.available();
  156.  
  157.     if (client) { // first byte
  158.         if (!alreadyConnected) {
  159.             client.flush();
  160.             Serial.println("New client");
  161.             client.println("Hi client!");
  162.             alreadyConnected = true;
  163.         }
  164.         if (client.available() > 0) {
  165.             // read incoming bytes from the client
  166.             int thisChar = client.read();
  167.             process(thisChar);
  168.             // echo back to the client
  169.             server.write(thisChar);
  170.         }
  171.         int x[] = {7, distance, pos};
  172.         for (int i = 0; i < 3; ++i) server.write(x[i]);
  173.     }
  174. }
  175.  
  176.  
  177. void printWifiStatus() {
  178.     Serial.print("SSID: ");
  179.     Serial.println(WiFi.SSID());
  180.  
  181.     IPAddress ip = WiFi.localIP();
  182.     Serial.print("IP Address: ");
  183.     Serial.println(ip);
  184.  
  185.     long rssi = WiFi.RSSI();
  186.     Serial.print("signal strength (RSSI):");
  187.     Serial.print(rssi);
  188.     Serial.println(" dBm");
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement