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