Advertisement
cymplecy

scratchpicobot

Dec 6th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.09 KB | None | 0 0
  1. /*
  2.   Code written by @cymplecy
  3.   Code based on TweetBot by @fortoffee
  4.   Built on MotorTest by 4Tronix
  5.  
  6.   This example code is licensed under CC-BY-SA https://creativecommons.org/licenses/by-sa/3.0/
  7.  */
  8. #define VERSION 0.1
  9. #include <FastLED.h>
  10. #include <SoftwareSerial.h>
  11.  
  12. #define L1 4  // Left motor pin1
  13. #define L2 5  // Left motor pin2
  14. #define R1 6  // Right motor pin1
  15. #define R2 7  // Right motor pin2
  16.  
  17. #define IR 6  // Right motor pin1
  18. #define IL 7  // Right motor pin2
  19. #define LED 13  // Blue LED pin
  20. #define DEBUG true
  21.  
  22. // WS2812B definitions
  23. #define NUM_LEDS 2
  24. #define DATA_PIN 9
  25. CRGB leds[NUM_LEDS];
  26. SoftwareSerial esp8266(2,3);
  27.  
  28. unsigned long previousMillis = 0;
  29. const long interval = 1000;
  30.  
  31. // the setup routine runs once when you press reset:
  32. void setup()
  33. {                
  34.   esp8266.begin(9600); // your esp's baud rate might be different
  35.   delay(100);
  36.   FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  37.   allOff();
  38.   // initialize the digital pins we will use as
  39.   pinMode(L1, OUTPUT);    
  40.   pinMode(L2, OUTPUT);    
  41.   pinMode(R1, OUTPUT);    
  42.   pinMode(R2, OUTPUT);
  43.   pinMode(LED, OUTPUT);
  44.   Serial.begin(115200);
  45.  
  46.   sendData("AT+RST\r\n", "\r\nready",2000,DEBUG); // reset module
  47.   sendData("AT+CWMODE=1\r\n",1000,DEBUG); // configure as station (client)
  48.   /*uncomment following line to attach to known AP. Config is
  49.     stored between resets/power cycles*/
  50.   sendData("AT+CWJAP=\"CYCY\",\"\"\r\n",30000,DEBUG); // connect to AP
  51.   sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
  52.   sendData("AT+CIPMUX=0\r\n",1000,DEBUG); // ensure single connection ONLY
  53.   sendData("AT+CIPSTART=\"TCP\",\"win8\",42001\r\n",1000,DEBUG);
  54.  
  55.   digitalWrite(L1, LOW);
  56.   digitalWrite(L2, LOW);
  57.   digitalWrite(R1, LOW);
  58.   digitalWrite(R2, LOW);
  59. }
  60.  
  61.  
  62. void loop()
  63. {
  64.  
  65.   String dataraw = "";
  66.   while(esp8266.available()) {
  67.     char c = esp8266.read();
  68.      dataraw += String(c);
  69.   }
  70.   if (dataraw.length() > 0) {
  71.     dataraw.toLowerCase();
  72.     //Serial.println("RAW:" + dataraw);
  73.     //Serial.println(dataraw.indexOf("colour"));
  74.     if (dataraw.indexOf("colour red") >=0)
  75.       setAll(255,0,0);
  76.     if (dataraw.indexOf("colour green") >=0)
  77.       setAll(0,255,0);    
  78.     if (dataraw.indexOf("colour blue") >=0)
  79.       setAll(0,0,255);    
  80.     if (dataraw.indexOf("colour white") >=0)
  81.       setAll(255,255,255);    
  82.     if (dataraw.indexOf("colour yellow") >=0)
  83.       setAll(255,255,0);
  84.     if (dataraw.indexOf("colour cyan") >=0)
  85.       setAll(0,255,255);    
  86.     if (dataraw.indexOf("colour magenta") >=0)
  87.       setAll(255,0,255);
  88.     if (dataraw.indexOf("colour orange") >=0)
  89.       setAll(255,128,0);    
  90.     if (dataraw.indexOf("colour purple") >=0)
  91.       setAll(128,0,128);      
  92.     if (dataraw.indexOf("colour pink") >=0)
  93.       setAll(255,0,255);
  94.     if (dataraw.indexOf("colour warmwhite") >=0)
  95.       setAll(255,255,255);    
  96.     if (dataraw.indexOf("colour off") >=0)
  97.       setAll(0,0,0);      
  98.     int motorA = dataraw.indexOf("motora");
  99.     if (motorA >=0) {
  100.       String power = dataraw.substring(motorA+8,motorA+12);
  101.       int powerInt = power.toInt();
  102.       Serial.println("PWR:" + power + "***");
  103.       if (powerInt > 0) {
  104.         analogWrite(L2, (255 - power.toInt()));
  105.         digitalWrite(L1, HIGH);
  106.       }
  107.       else if (powerInt < 0) {
  108.         analogWrite(L2, (255 - power.toInt()));
  109.         digitalWrite(L1, LOW);
  110.       }
  111.       else {
  112.       digitalWrite(L2, LOW);
  113.       digitalWrite(L1, LOW);
  114.       }
  115.     }
  116.  
  117.     int motorB = dataraw.indexOf("motorb");
  118.     if (motorB >=0) {
  119.       String power = dataraw.substring(motorB+8,motorB+12);
  120.       int powerInt = power.toInt();
  121.       Serial.println("PWR:" + power + "***");
  122.       if (powerInt > 0) {
  123.         analogWrite(R1, (255 - power.toInt()));
  124.         digitalWrite(R2, HIGH);
  125.       }
  126.       else if (powerInt < 0){
  127.         analogWrite(R1, (255 - power.toInt()));
  128.         digitalWrite(R2, LOW);
  129.       }
  130.       else {
  131.         digitalWrite(R1, LOW);
  132.         digitalWrite(R2, LOW);
  133.       }
  134.     }    
  135.  
  136.   }
  137.   else {
  138.     String cmd;
  139.     int cmdLen = 0;
  140.     unsigned long currentMillis = millis();
  141.     if (currentMillis - previousMillis >= interval) {
  142.       // save the last time you blinked the LED
  143.       previousMillis = currentMillis;
  144.       cmd = ("sensor-update \"leftldr\" " + String(analogRead(0)) + " \"rightldr\" " + String(analogRead(1)) + " \"leftline\" " + String(analogRead(6)) + " \"rightline\" " + String(analogRead(7)));
  145.       cmdLen = cmd.length();
  146.       esp8266.print("AT+CIPSEND=");
  147.       esp8266.println((cmd.length() + 4) );
  148.       if (esp8266.find(">")){
  149.         //Serial.print(">");
  150.         //erial.print(cmd);
  151.         esp8266.write(byte(0));
  152.         esp8266.write(byte(0));
  153.         esp8266.write(byte(0));
  154.         esp8266.write(byte(cmd.length()));
  155.         esp8266.print(cmd);
  156.  
  157.         if(esp8266.find("OK")){
  158.           //Serial.println("OK");
  159.         }else{
  160.           Serial.println("Error");
  161.         }
  162.       }
  163.     }
  164.   }
  165. }
  166. String sendData(String command, const int timeout, boolean debug)
  167.   {
  168.     return sendData(command, "OK", timeout, debug);
  169.   }    
  170.  
  171. String sendData(String command, String waitFor, const int timeout, boolean debug)
  172. {
  173.     String response = "";
  174.    
  175.     esp8266.print(command); // send the read character to the esp8266
  176.    
  177.     long int time = millis();
  178.     bool exit = false;
  179.     while( ((time+timeout) > millis()) && (!exit))
  180.     {
  181.       while((esp8266.available()) && (!exit))
  182.       {
  183.        
  184.         // The esp has data so display its output to the serial window
  185.         char c = esp8266.read(); // read the next character.
  186.         response+=c;
  187.         if (response.indexOf(waitFor) > -1)
  188.           exit = true;  //found data terminator
  189.       }  
  190.     }
  191.    
  192.     if(debug)
  193.     {
  194.       Serial.print(response);
  195.     }
  196.    
  197.     return response;
  198. }
  199. void allOff()
  200. {
  201.   for (int i=0; i<NUM_LEDS; i++)
  202.     leds[i] = 0;
  203.   FastLED.show();
  204. }
  205.  
  206. void setAll(int red, int green, int blue)
  207. {
  208.   for (int i=0; i<NUM_LEDS; i++)
  209.   {
  210.     leds[i].g = red;
  211.     leds[i].r = green;
  212.     leds[i].b = blue;
  213.   }
  214.   FastLED.show();
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement