Components and supplies
GPS Neo 5,6,7
Arduino UNO Wifi Rev.2
Apps and platforms
Arduino IDE
Project description
Code
Just getting started, not a pro dont judge
arduino
testing these individual parts as we progress
1#include <LiquidCrystal.h> 2#include <SoftwareSerial.h> 3#include <TinyGPS.h> 4#include <Math.h> 5 6 7 8 9 10 11 char t; // TESTING VARIBLE TO DRIVE ROBOT s stop L left R right B Backwards 12 const int l1=10,l2=11,r1=12,r2=13; /// motor driving pins connected to relay 13 14 15 // written by Josef McInturff 16 17 //long lat,lon; // create variable for latitude and longitude object 18 float lat = 28.54,lon = 77.17; // create variable for latitude and longitude object 19 SoftwareSerial gpsSerial(4,3); //rx,tx 20 LiquidCrystal lcd(A0,A1,A2,A3,A4,A5); ///LCD PINS LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 21 TinyGPS gps; // create gps object 22 23 24 25 26 27void setup() { 28 29 30 Serial.begin(9600); 31 32 //Serial.println("The GPS Received Signal:"); 33 gpsSerial.begin(9600); // connect gps sensor 34 lcd.begin(16,2); 35 36 37 38 39 40 41 42 43 44 45 46 47 /// setup for driving the robot below 48 49 pinMode(r1,OUTPUT); //right motors forward 50 pinMode(r2,OUTPUT); //right motors reverse 51 pinMode(l1,OUTPUT); //left motors forward 52 pinMode(l2,OUTPUT); //left motors reverse 53 54 digitalWrite (r1,HIGH); //low activates relays i use set all to high 55 digitalWrite (r2,HIGH); 56 digitalWrite (l1,HIGH); 57 digitalWrite (l2,HIGH); 58 59 60} 61 62void loop() { 63 64 65 66///setting up GPS On LCD display 67 68 69 while(gpsSerial.available()){ // check for gps data 70 if(gps.encode(gpsSerial.read()))// encode gps data 71 72 gps.f_get_position(&lat,&lon); // get latitude and longitude 73 // display position 74 lcd.clear(); 75 lcd.setCursor(1,0); 76 lcd.print("GPS Signal"); 77 //Serial.print("Position: "); 78 //Serial.print("Latitude:"); 79 //Serial.print(lat,6); 80 //Serial.print(";"); 81 //Serial.print("Longitude:"); 82 //Serial.println(lon,6); 83 lcd.setCursor(1,0); 84 lcd.print("LAT:"); 85 lcd.setCursor(5,0); 86 lcd.print(lat); 87 //Serial.print(lat); 88 //Serial.print(" "); 89 90 lcd.setCursor(0,1); 91 lcd.print(",LON:"); 92 lcd.setCursor(5,1); 93 lcd.print(lon); 94} 95 96 97 98 String latitude = String(lat,6); 99 String longitude = String(lon,6); 100 Serial.println(latitude+";"+longitude); 101 delay(1000); 102 103 104 105 106 107 108 109// driving instructions below 110 111char t = 'R'; // TESTING VARIBLE TO DRIVE ROBOT s stop L left R right B Backwards 112 113 114 115if(Serial.available()){ 116 t = Serial.read(); 117 } 118 119if(t == 'F'){ //move forward(all motors rotate in forward direction) 120 digitalWrite(r1,LOW); 121 digitalWrite(r2,HIGH); 122 digitalWrite(l1,LOW); 123 digitalWrite(l2,HIGH); 124 delay(1000); 125 t = 'S'; 126 } 127 128else if(t == 'B'){ //move reverse (all motors rotate in reverse direction) 129 digitalWrite(r1,HIGH); 130 digitalWrite(r2,LOW); 131 digitalWrite(l1,HIGH); 132 digitalWrite(l2,LOW); 133 delay(1000); 134 t = 'S'; 135 } 136 137else if(t == 'L'){ //turn right (left side motors rotate in forward direction, right side motors rotate in reverse direction for 100ms & stop) 138 digitalWrite(r1,LOW); 139 digitalWrite(r2,HIGH); 140 digitalWrite(l1,HIGH); 141 digitalWrite(l2,LOW); 142 delay(1000); 143 t = 'S'; 144 } 145 146else if(t == 'R'){ //turn left (right side motors rotate in forward direction, left side motors rotate in reverse direction for 100ms & stop) 147 digitalWrite(r1,HIGH); 148 digitalWrite(r2,LOW); 149 digitalWrite(l1,LOW); 150 digitalWrite(l2,HIGH); 151 delay(1000); 152 t = 'S'; 153 } 154 155else if(t == 'S'){ //STOP (all motors stop) 156 digitalWrite(r1,HIGH); 157 digitalWrite(r2,HIGH); 158 digitalWrite(l1,HIGH); 159 digitalWrite(l2,HIGH); 160 } 161delay(100); 162 163 } 164
Just getting started, not a pro dont judge
arduino
1#include <TinyGPS++.h> 2#include <SoftwareSerial.h> 3 4created by josef mcinturff Common Creative CC 5 6#include <SPI.h> /// wifi script 7#include <WiFiNINA.h> /// wifi script 8#define wifiConnect 8 /// wifi script 9 10char ssid[] = "YOUR WIFI NAME"; /// wifi script 11char pass[] = "Your PASSWORD"; /// wifi script 12int keyIndex = 0; /// wifi script 13 14int status = WL_IDLE_STATUS; /// wifi script 15 16WiFiServer server(80); /// wifi script 17 18 19 20 21 22 23static const int RXPin = 3 , TXPin = 4; 24static const uint32_t GPSBaud = 9600; 25 26 // The TinyGPS++ object of Josef McInturff 27TinyGPSPlus gps; 28 29 // The serial connection to the GPS device 30SoftwareSerial ss(RXPin, TXPin); 31 32 33 char t; // TESTING VARIBLE TO DRIVE ROBOT s stop L left R right B Backwards 34 const int l1=10,l2=11,r1=12,r2=13; /// motor driving pins connected to relay 35 36 37 38void setup(){ 39 40 //Initialize serial and wait for port to open: 41 Serial.begin(9600); 42 while (!Serial) { 43 ; // wait for serial port to connect. Needed for native USB port only 44 } 45 46 47 48 /// setup for driving the robot below 49 50 pinMode(r1,OUTPUT); // R1 right motors forward 51 pinMode(r2,OUTPUT); // R2 right motors reverse 52 pinMode(l1,OUTPUT); // L1 left motors forward 53 pinMode(l2,OUTPUT); // L2 left motors reverse 54 55 digitalWrite (r1,HIGH); //low activates relays i use set all to high 56 digitalWrite (r2,HIGH); 57 digitalWrite (l1,HIGH); 58 digitalWrite (l2,HIGH); 59 60 Serial.begin(9600); 61 ss.begin(GPSBaud); 62 63 64 65} 66 67void loop(){ 68 69 70 71 72 73 74 75 76 // put your main code here, to run repeatedly: 77 78 79// LOW signal of arduino aktivates relay: 80 81 82 // This sketch displays information every time a new sentence is correctly encoded. 83 while (ss.available() > 0){ 84 gps.encode(ss.read()); 85 if (gps.location.isUpdated()){ 86 Serial.print("Latitude= "); 87 Serial.print(gps.location.lat(), 6); 88 Serial.print(" Longitude= "); 89 Serial.println(gps.location.lng(), 6); 90 } 91 } 92 93// driving instructions below 94 95// char t = 'F'; // TESTING VARIBLE TO DRIVE ROBOT s stop L left R right B Backwards 96 97 98 99if(Serial.available()){ 100 t = Serial.read(); 101 } 102 103// R1 right motors forward 104// R2 right motors reverse 105// L1 left motors forward 106// L2 left motors reverse 107 108 109if(t == 'F'){ //move forward(all motors rotate in forward direction) 110 digitalWrite(r1,LOW); 111 digitalWrite(r2,HIGH); 112 digitalWrite(l1,LOW); 113 digitalWrite(l2,HIGH); 114 delay(1000); 115 t = 'S'; 116 } 117 118else if(t == 'B'){ //move reverse (all motors rotate in reverse direction) 119 digitalWrite(r1,HIGH); 120 digitalWrite(r2,LOW); 121 digitalWrite(l1,HIGH); 122 digitalWrite(l2,LOW); 123 delay(1000); 124 t = 'S'; 125 } 126 127else if(t == 'L'){ //turn right (left side motors rotate in forward direction, right side motors rotate in reverse direction for 100ms & stop) 128 digitalWrite(r1,LOW); 129 digitalWrite(r2,HIGH); 130 digitalWrite(l1,HIGH); 131 digitalWrite(l2,LOW); 132 delay(1000); 133 t = 'S'; 134 } 135 136else if(t == 'R'){ //turn left (right side motors rotate in forward direction, left side motors rotate in reverse direction for 100ms & stop) 137 digitalWrite(r1,HIGH); 138 digitalWrite(r2,LOW); 139 digitalWrite(l1,LOW); 140 digitalWrite(l2,HIGH); 141 delay(1000); 142 t = 'S'; 143 } 144 145else if(t == 'S'){ //STOP (all motors stop) 146 digitalWrite(r1,HIGH); 147 digitalWrite(r2,HIGH); 148 digitalWrite(l1,HIGH); 149 digitalWrite(l2,HIGH); 150 } 151delay(100); 152 153 154 155} 156
UPDATED WITH sd card SHIELD NOW
arduino
CODE UPDATED AND PIN CONFIGURATION CHANGED !!!
1#include <TinyGPS++.h> 2#include <SoftwareSerial.h> 3 4#include <SPI.h> /// sd card reader 5#include <SD.h> /// sd card reader 6 7#include <SPI.h> /// wifi script 8#include <WiFiNINA.h> /// wifi script 9#define wifiConnect 8 /// wifi script 10 11char ssid[] = "WIFI NETWORK NAME"; /// wifi script 12char pass[] = "WIFI PASSWORD"; /// wifi script 13int keyIndex = 0; /// wifi script 14 15int status = WL_IDLE_STATUS; /// wifi script 16 17WiFiServer server(80); /// wifi script 18 19String command; ///part of beeing able to send commands from terminal line to drive 20 21 22 23 24 25static const int RXPin = 0 , TXPin = 1; 26static const uint32_t GPSBaud = 9600; 27 28 // The TinyGPS++ object of Josef McInturff 29TinyGPSPlus gps; 30 31 // The serial connection to the GPS device 32SoftwareSerial ss(RXPin, TXPin); 33 34 35 const int l1=2,l2=3,r1=5,r2=6; /// motor driving pins connected to relay 36 37/// pin 4 is required to be used by my SD card shield 38// ** MOSI - pin 11 39// ** MISO - pin 12 40// ** CLK - pin 13 41// ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN) 42const int chipSelect = 4; 43 44void setup(){ 45 46 pinMode(wifiConnect, OUTPUT); /// wifi script 47 48 49 //Initialize serial and wait for port to open: 50 Serial.begin(9600); 51 while (!Serial) { 52 ; // wait for serial port to connect. Needed for native USB port only 53 } 54Serial.print("Initializing SD card..."); //// SD Card script 55 if (!SD.begin(chipSelect)) { 56 Serial.println("Card failed, or not present"); 57 // don't do anything more: 58 while (1); 59 } 60 Serial.println("card initialized."); 61 62 /// setup for driving the robot below 63 64 pinMode(r1,OUTPUT); // R1 right motors forward 65 pinMode(r2,OUTPUT); // R2 right motors reverse 66 pinMode(l1,OUTPUT); // L1 left motors forward 67 pinMode(l2,OUTPUT); // L2 left motors reverse 68 69 digitalWrite (r1,HIGH); //low activates relays i use set all to high 70 digitalWrite (r2,HIGH); 71 digitalWrite (l1,HIGH); 72 digitalWrite (l2,HIGH); 73 74 Serial.begin(9600); 75 ss.begin(GPSBaud); 76 77while (status != WL_CONNECTED) { /// wifi script 78 Serial.print("Attempting to connect to Network named: "); /// wifi script 79 Serial.println(ssid); /// wifi script 80 status = WiFi.begin(ssid, pass); /// wifi script 81 delay(10000); /// wifi script 82 } /// wifi script 83 84 server.begin(); /// wifi script 85 86 digitalWrite(wifiConnect, HIGH); /// wifi script 87 88 // Serial.print("SSID: "); /// wifi script 89 // Serial.println(WiFi.SSID()); /// wifi script 90 IPAddress ip = WiFi.localIP(); /// wifi script 91 Serial.print("IP Address: "); /// wifi script 92 Serial.println(ip); /// wifi script 93 94} 95 96void loop(){ 97 98 99//Serial.print("Type Command ( STOP, FORWARD, BACK, LEFT, RIGHT )"); /// sending commands via terminal 100 // sd card script 101 102 103 104 // put your main code here, to run repeatedly: 105 106 107// LOW signal of arduino aktivates relay: 108 109 110 // This sketch displays information every time a new sentence is correctly encoded. 111 while (ss.available() > 0){ 112 gps.encode(ss.read()); 113 if (gps.location.isUpdated()){ 114 Serial.print("Latitude= "); 115 Serial.print(gps.location.lat(), 6); 116 Serial.print(" Longitude= "); 117 Serial.println(gps.location.lng(), 6); 118 119 Serial.print("SSID: "); /// wifi script 120 Serial.println(WiFi.SSID()); /// wifi script 121 IPAddress ip = WiFi.localIP(); /// wifi script 122 Serial.print("IP Address: "); /// wifi script 123 Serial.println(ip); /// wifi script 124 // print sensor to the serial port too: 125 126 127 } 128 129 String dataString = ""; // sd card script 130 131 // read three sensors and append to the string: 132 for (int analogPin = 0; 133 analogPin < 3; 134 analogPin++) { // sd card script 135 int sensor = analogRead(analogPin); // sd card script 136 dataString += String(sensor); // sd card script 137 if (analogPin < 2) { // sd card script 138 dataString += ",";} 139// open the file. note that only one file can be open at a time, 140 // so you have to close this one before opening another. 141File dataFile = SD.open("datalog.txt", FILE_WRITE); 142 143 // if the file is available, write to it: 144 if (dataFile) { 145 dataFile.println(dataString); 146 dataFile.close(); 147 } 148 // if the file isn't open, pop up an error: 149 else { 150 Serial.println("error opening datalog.txt Replace SD card reboot"); 151 } 152 153 154 155 156 157 } 158 159 160 161 162 163 164 165 166 167// driving instructions below 168if(Serial.available()){ 169} 170 171 if (Serial.available()) { 172 command = Serial.readStringUntil('\ 173'); 174 command.trim(); 175 if (command.equals("STOP")) { 176 digitalWrite(r1, HIGH); 177 digitalWrite(r2, HIGH); 178 digitalWrite(l1, HIGH); 179 digitalWrite(l2, HIGH); 180 delay(1000); 181 } 182 183 else if (command.equals("FORWARD")) { 184 digitalWrite(r1, LOW); 185 digitalWrite(r2, HIGH); 186 digitalWrite(l1, LOW); 187 digitalWrite(l2, HIGH); 188 delay(5000); 189 190 } 191 192 else if (command.equals("BACK")) { 193 digitalWrite(r1, HIGH); 194 digitalWrite(r2, LOW); 195 digitalWrite(l1, HIGH); 196 digitalWrite(l2, LOW); 197 delay(5000); 198 } 199 else if (command.equals("LEFT")) { 200 digitalWrite(r1, LOW); 201 digitalWrite(r2, HIGH); 202 digitalWrite(l1, HIGH); 203 digitalWrite(l2, LOW); 204 delay(2000); 205 } 206 else if (command.equals("RIGHT")) { 207 digitalWrite(r1, HIGH); 208 digitalWrite(r2, LOW); 209 digitalWrite(l1, LOW); 210 digitalWrite(l2, HIGH); 211 delay(2000); 212 } 213 214 else if (command.equals("ROW ")) { 215 digitalWrite(r1, LOW); 216 digitalWrite(r2, HIGH); 217 digitalWrite(l1, LOW); 218 digitalWrite(l2, HIGH); 219 delay(500); 220 } 221 else { 222 Serial.println("bad command"); 223 } 224 Serial.print("Command: "); 225 Serial.println(command); 226 } 227 228 229 } 230} 231 232 233
Soil sampler code
arduino
in this sketch we push a button to display current soil readings
1int toggleState; 2int buttonStatus = 0; 3 4 5void setup() { 6 // initialize serial communication at 9600 bits per second: 7 8 Serial.begin(9600); 9 pinMode(9, OUTPUT); // turn water relay on "needs water" 10 pinMode(10, OUTPUT); // turn water relay on "needs nutrients" 11 // this sketch also uses Analog pin 1 water sensing sensor value over 1000 under 900 12 // this sketch also uses Analog pin 2 nutrients sensing 13 pinMode(8, INPUT_PULLUP); // enable internal pull-up pin 8 will be used as trigger by the {gps target plant reached} pin Later pullup may need to bemoved later 14 15 // pin 8 is currently a push botton but may later be set by "target arrived" function of my gps i also want to use this pin to take instant reading where the head unit is currently 16 17 Serial.begin(9600); 18 Serial.println("please push botton to trigger sensor reading"); 19} 20 21 22 23// the loop routine runs over and over again forever: 24void loop() { 25 26 // Set LED colour to their Digital Ouputs. Read the input on analog pin 0: 27 int water = 9; //feeding pin to run pump for water 28 int nutrients = 10; // feeding pin for nutrients 29 int sensorValueWater = analogRead(A1); 30 int sensorValueNutrients = analogRead(A2); 31 32 int pinValue = digitalRead(8); ///Pushbotton code 33 delay(10); // quick and dirty debounce filter 34 if (buttonStatus != pinValue) { ///Pushbotton code 35 buttonStatus = pinValue; ///Pushbotton code 36 Serial.println(buttonStatus); ///Pushbotton code 37 } 38 // Set the initial state of the LEDs to OFF 39 digitalWrite(9, HIGH); //feeding pin to run pump for water Low activates relay 40 digitalWrite(10, HIGH); // feeding pin for nutrients low activated relay 41 42 43 44 45if(buttonStatus > 0) { //// in this loop we take readings and set relay to water and nutrients dispensary 46 47 // Logic Loop that sets the required LED to ON 48 if (sensorValueWater >= 1000) (digitalWrite(water, LOW)); ////watering code 49 else if ((sensorValueWater <= 999) && (sensorValueWater >=901)) (digitalWrite(water, HIGH)); ////watering code 50 else if (sensorValueWater <= 900) (digitalWrite(water, HIGH)); ////watering code 51 else ; 52 53 54 // Prints the condition of soil. Dry, Wet or Perfect 55 if (sensorValueWater >= 1000) (Serial.print("SOIL IS TOO DRY!!!!! watering now ")); ////watering code 56 else if ((sensorValueWater<= 999) && (sensorValueWater >=901)) (Serial.print("SOIL IS PERFECT!!!!! ")); ////watering code 57 else if (sensorValueWater <= 900) (Serial.print("SOIL IS TOO WET!!!!! ")); ////watering code 58 else; 59 60 61 // print out the value you read: 62 Serial.print("Soil Humidity: "); 63 Serial.println(sensorValueWater); 64 delay(1500); // delay in between reads for stability and keep water and nutrients on for this delay 65 digitalWrite(9, HIGH); //feeding pin to run pump for water 66 67 68 69 // Logic Loop that sets the required LED to ON 70 if (sensorValueNutrients >= 1000) (digitalWrite(nutrients, LOW)); ////nutirents code 71 else if ((sensorValueNutrients <= 999) && (sensorValueNutrients >=901)) (digitalWrite(nutrients, HIGH)); ////nutrients code 72 else if (sensorValueNutrients <= 900) (digitalWrite(nutrients, HIGH)); ////nutrients code 73 else ; 74 75 76 // Prints the condition of soil. Dry, Wet or Perfect 77 if (sensorValueNutrients >= 1000) (Serial.print("nutrients are LOW dispensing now ")); ////watering code 78 else if ((sensorValueNutrients<= 999) && (sensorValueNutrients >=901)) (Serial.print("SOIL IS PERFECT!!!!! ")); ////watering code 79 else if (sensorValueNutrients <= 900) (Serial.print("SOIL IS TOO HOT IN NUTRIENTS!!! ")); ////watering code 80 else; 81 82 83 84 // print out the value you read: 85 Serial.print("Nutrients: "); 86 Serial.println(sensorValueNutrients); 87 delay(1500); // delay in between reads for stability and keep water and nutrients on for this delay 88 digitalWrite(10, HIGH); // feeding pin for nutrients 89 90 91 92 } 93 94 95 96 while(buttonStatus >= 1) { // this code makes sure we are only running samples once after pushing putton nat as long as button is pushed manually (gps location arrived i dnt want to run this sketch non stop) 97 toggleState=! toggleState; 98 digitalWrite(8, toggleState); 99 delay(200) ; 100 buttonStatus = digitalRead (8); 101 } 102 toggleState =! toggleState; 103 digitalWrite (8, toggleState); 104 delay(200); 105 106 107 108 109}
FUNCTIONING SERIAL READ OUT
arduino
WORKING CODE SERIAL READ OUT OF ip WIFI NETWORK gps LOCATION RECIVES DRIVING DRIRECTION FROM COMMAND LINE
1#include <TinyGPS++.h> 2#include <SoftwareSerial.h> 3 4 5#include 6 <SPI.h> /// wifi script 7#include <WiFiNINA.h> /// 8 wifi script 9#define wifiConnect 8 /// wifi script 10 11char 12 ssid[] = "wifi network name"; /// wifi script 13char pass[] 14 = "wifi NETWORK PASSWORD"; /// wifi script 15int keyIndex 16 = 0; /// wifi script 17 18int status = WL_IDLE_STATUS; 19 /// wifi script 20 21WiFiServer server(80); /// 22 wifi script 23 24String command; ///part of beeing able 25 to send commands from terminal line to drive 26 27 28 29 30 31static const 32 int RXPin = 3 , TXPin = 4; 33static const uint32_t GPSBaud = 9600; 34 35 // 36 The TinyGPS++ object of Josef McInturff 37TinyGPSPlus gps; 38 39 // 40 The serial connection to the GPS device 41SoftwareSerial ss(RXPin, TXPin); 42 43 44 45 char t; // TESTING VARIBLE TO DRIVE ROBOT s 46 stop L left R right B Backwards 47 const int l1=10,l2=11,r1=12,r2=13; 48 /// motor driving pins connected to relay 49 50 51 52void setup(){ 53 54 55 pinMode(wifiConnect, OUTPUT); /// wifi script 56 57 58 //Initialize 59 serial and wait for port to open: 60 Serial.begin(9600); 61 while (!Serial) 62 { 63 ; // wait for serial port to connect. Needed for native USB port only 64 65 } 66 67 68 69 /// setup for driving the robot below 70 71 72 pinMode(r1,OUTPUT); // R1 right motors forward 73 pinMode(r2,OUTPUT); 74 // R2 right motors reverse 75 pinMode(l1,OUTPUT); // L1 left motors forward 76 77 pinMode(l2,OUTPUT); // L2 left motors reverse 78 79 digitalWrite (r1,HIGH); 80 //low activates relays i use set all to high 81 digitalWrite (r2,HIGH); 82 83 digitalWrite (l1,HIGH); 84 digitalWrite (l2,HIGH); 85 86 Serial.begin(9600); 87 88 ss.begin(GPSBaud); 89 90while (status != WL_CONNECTED) { /// wifi 91 script 92 Serial.print("Attempting to connect to Network named: "); /// 93 wifi script 94 Serial.println(ssid); /// wifi script 95 status 96 = WiFi.begin(ssid, pass); /// wifi script 97 delay(10000); /// 98 wifi script 99 } /// wifi script 100 101 server.begin(); /// 102 wifi script 103 104 digitalWrite(wifiConnect, HIGH); /// wifi script 105 106 107 Serial.print("SSID: "); /// wifi script 108 Serial.println(WiFi.SSID()); 109 /// wifi script 110 IPAddress ip = WiFi.localIP(); /// 111 wifi script 112 Serial.print("IP Address: "); /// wifi script 113 114 Serial.println(ip); /// wifi script 115 116} 117 118void loop(){ 119 120 121 122 123 124 125 126 // put your main code here, to run repeatedly: 127 128 129// LOW signal 130 of arduino aktivates relay: 131 132 133 // This sketch displays information every 134 time a new sentence is correctly encoded. 135 while (ss.available() > 0){ 136 gps.encode(ss.read()); 137 138 if (gps.location.isUpdated()){ 139 Serial.print("Latitude= "); 140 Serial.print(gps.location.lat(), 141 6); 142 Serial.print(" Longitude= "); 143 Serial.println(gps.location.lng(), 144 6); 145 146 Serial.print("SSID: "); /// wifi script 147 Serial.println(WiFi.SSID()); 148 /// wifi script 149 IPAddress ip = WiFi.localIP(); /// 150 wifi script 151 Serial.print("IP Address: "); /// wifi script 152 153 Serial.println(ip); /// wifi script 154 155 156 } 157 } 158 159// 160 driving instructions below 161 162// char t = 'F'; // TESTING VARIBLE 163 TO DRIVE ROBOT s stop L left R right B Backwards 164 165 166 167if(Serial.available()){ 168 169 t = Serial.read(); 170 } 171 172// R1 right motors forward 173// R2 right motors 174 reverse 175// L1 left motors forward 176// L2 left motors reverse 177 178 179if(t 180 == 'F'){ //move forward(all motors rotate in forward direction) 181 digitalWrite(r1,LOW); 182 183 digitalWrite(r2,HIGH); 184 digitalWrite(l1,LOW); 185 digitalWrite(l2,HIGH); 186 187 delay(1000); 188 t = 'S'; 189 } 190 191else if(t == 'B'){ //move reverse 192 (all motors rotate in reverse direction) 193 digitalWrite(r1,HIGH); 194 digitalWrite(r2,LOW); 195 196 digitalWrite(l1,HIGH); 197 digitalWrite(l2,LOW); 198 delay(1000); 199 t = 'S'; 200 201 } 202 203else if(t == 'L'){ //turn right (left side motors rotate in forward 204 direction, right side motors rotate in reverse direction for 100ms & stop) 205 digitalWrite(r1,LOW); 206 207 digitalWrite(r2,HIGH); 208 digitalWrite(l1,HIGH); 209 digitalWrite(l2,LOW); 210 211 delay(1000); 212 t = 'S'; 213 } 214 215else if(t == 'R'){ //turn left 216 (right side motors rotate in forward direction, left side motors rotate in reverse 217 direction for 100ms & stop) 218 digitalWrite(r1,HIGH); 219 digitalWrite(r2,LOW); 220 221 digitalWrite(l1,LOW); 222 digitalWrite(l2,HIGH); 223 delay(1000); 224 t = 'S'; 225 226 } 227 228else if(t == 'S'){ //STOP (all motors stop) 229 digitalWrite(r1,HIGH); 230 231 digitalWrite(r2,HIGH); 232 digitalWrite(l1,HIGH); 233 digitalWrite(l2,HIGH); 234 235 } 236delay(100); 237 238 239 240 Serial.println("Type Command ( STOP, FORWARD, 241 BACK, LEFT, RIGHT )"); /// sending commands via terminal 242 243delay(1000); 244 245 246 if (Serial.available()) { 247 command = Serial.readStringUntil('\ 248'); 249 250 command.trim(); 251 if (command.equals("STOP")) { 252 digitalWrite(r1, 253 HIGH); 254 digitalWrite(r2, HIGH); 255 digitalWrite(l1, HIGH); 256 digitalWrite(l2, 257 HIGH); 258delay(1000); 259 } 260 261 else if (command.equals("FORWARD")) 262 { 263 digitalWrite(r1, LOW); 264 digitalWrite(r2, HIGH); 265 digitalWrite(l1, 266 LOW); 267 digitalWrite(l2, HIGH); 268delay(5000); 269 270 271 } 272 273 else if (command.equals("BACK")) { 274 digitalWrite(r1, 275 HIGH); 276 digitalWrite(r2, LOW); 277 digitalWrite(l1, HIGH); 278 digitalWrite(l2, 279 LOW); 280delay(5000); 281 282 } 283 else if (command.equals("LEFT")) 284 { 285 digitalWrite(r1, LOW); 286 digitalWrite(r2, HIGH); 287 digitalWrite(l1, 288 HIGH); 289 digitalWrite(l2, LOW); 290delay(2000); 291} 292 else if 293 (command.equals("RIGHT")) { 294 digitalWrite(r1, HIGH); 295 digitalWrite(r2, 296 LOW); 297 digitalWrite(l1, LOW); 298 digitalWrite(l2, HIGH); 299delay(2000); 300} 301 302 303 else if (command.equals("ROW ")) { 304 digitalWrite(r1, LOW); 305 digitalWrite(r2, 306 HIGH); 307 digitalWrite(l1, LOW); 308 digitalWrite(l2, HIGH); 309delay(500); 310 311 312 } 313 else { 314 Serial.println("bad command"); 315 } 316 317 Serial.print("Command: "); 318 Serial.println(command); 319 } 320 321 322 323 324} 325
UPDATED WITH sd card SHIELD NOW
arduino
CODE UPDATED AND PIN CONFIGURATION CHANGED !!!
1#include <TinyGPS++.h> 2#include <SoftwareSerial.h> 3 4#include <SPI.h> /// sd card reader 5#include <SD.h> /// sd card reader 6 7#include <SPI.h> /// wifi script 8#include <WiFiNINA.h> /// wifi script 9#define wifiConnect 8 /// wifi script 10 11char ssid[] = "WIFI NETWORK NAME"; /// wifi script 12char pass[] = "WIFI PASSWORD"; /// wifi script 13int keyIndex = 0; /// wifi script 14 15int status = WL_IDLE_STATUS; /// wifi script 16 17WiFiServer server(80); /// wifi script 18 19String command; ///part of beeing able to send commands from terminal line to drive 20 21 22 23 24 25static const int RXPin = 0 , TXPin = 1; 26static const uint32_t GPSBaud = 9600; 27 28 // The TinyGPS++ object of Josef McInturff 29TinyGPSPlus gps; 30 31 // The serial connection to the GPS device 32SoftwareSerial ss(RXPin, TXPin); 33 34 35 const int l1=2,l2=3,r1=5,r2=6; /// motor driving pins connected to relay 36 37/// pin 4 is required to be used by my SD card shield 38// ** MOSI - pin 11 39// ** MISO - pin 12 40// ** CLK - pin 13 41// ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN) 42const int chipSelect = 4; 43 44void setup(){ 45 46 pinMode(wifiConnect, OUTPUT); /// wifi script 47 48 49 //Initialize serial and wait for port to open: 50 Serial.begin(9600); 51 while (!Serial) { 52 ; // wait for serial port to connect. Needed for native USB port only 53 } 54Serial.print("Initializing SD card..."); //// SD Card script 55 if (!SD.begin(chipSelect)) { 56 Serial.println("Card failed, or not present"); 57 // don't do anything more: 58 while (1); 59 } 60 Serial.println("card initialized."); 61 62 /// setup for driving the robot below 63 64 pinMode(r1,OUTPUT); // R1 right motors forward 65 pinMode(r2,OUTPUT); // R2 right motors reverse 66 pinMode(l1,OUTPUT); // L1 left motors forward 67 pinMode(l2,OUTPUT); // L2 left motors reverse 68 69 digitalWrite (r1,HIGH); //low activates relays i use set all to high 70 digitalWrite (r2,HIGH); 71 digitalWrite (l1,HIGH); 72 digitalWrite (l2,HIGH); 73 74 Serial.begin(9600); 75 ss.begin(GPSBaud); 76 77while (status != WL_CONNECTED) { /// wifi script 78 Serial.print("Attempting to connect to Network named: "); /// wifi script 79 Serial.println(ssid); /// wifi script 80 status = WiFi.begin(ssid, pass); /// wifi script 81 delay(10000); /// wifi script 82 } /// wifi script 83 84 server.begin(); /// wifi script 85 86 digitalWrite(wifiConnect, HIGH); /// wifi script 87 88 // Serial.print("SSID: "); /// wifi script 89 // Serial.println(WiFi.SSID()); /// wifi script 90 IPAddress ip = WiFi.localIP(); /// wifi script 91 Serial.print("IP Address: "); /// wifi script 92 Serial.println(ip); /// wifi script 93 94} 95 96void loop(){ 97 98 99//Serial.print("Type Command ( STOP, FORWARD, BACK, LEFT, RIGHT )"); /// sending commands via terminal 100 // sd card script 101 102 103 104 // put your main code here, to run repeatedly: 105 106 107// LOW signal of arduino aktivates relay: 108 109 110 // This sketch displays information every time a new sentence is correctly encoded. 111 while (ss.available() > 0){ 112 gps.encode(ss.read()); 113 if (gps.location.isUpdated()){ 114 Serial.print("Latitude= "); 115 Serial.print(gps.location.lat(), 6); 116 Serial.print(" Longitude= "); 117 Serial.println(gps.location.lng(), 6); 118 119 Serial.print("SSID: "); /// wifi script 120 Serial.println(WiFi.SSID()); /// wifi script 121 IPAddress ip = WiFi.localIP(); /// wifi script 122 Serial.print("IP Address: "); /// wifi script 123 Serial.println(ip); /// wifi script 124 // print sensor to the serial port too: 125 126 127 } 128 129 String dataString = ""; // sd card script 130 131 // read three sensors and append to the string: 132 for (int analogPin = 0; 133 analogPin < 3; 134 analogPin++) { // sd card script 135 int sensor = analogRead(analogPin); // sd card script 136 dataString += String(sensor); // sd card script 137 if (analogPin < 2) { // sd card script 138 dataString += ",";} 139// open the file. note that only one file can be open at a time, 140 // so you have to close this one before opening another. 141File dataFile = SD.open("datalog.txt", FILE_WRITE); 142 143 // if the file is available, write to it: 144 if (dataFile) { 145 dataFile.println(dataString); 146 dataFile.close(); 147 } 148 // if the file isn't open, pop up an error: 149 else { 150 Serial.println("error opening datalog.txt Replace SD card reboot"); 151 } 152 153 154 155 156 157 } 158 159 160 161 162 163 164 165 166 167// driving instructions below 168if(Serial.available()){ 169} 170 171 if (Serial.available()) { 172 command = Serial.readStringUntil('\n'); 173 command.trim(); 174 if (command.equals("STOP")) { 175 digitalWrite(r1, HIGH); 176 digitalWrite(r2, HIGH); 177 digitalWrite(l1, HIGH); 178 digitalWrite(l2, HIGH); 179 delay(1000); 180 } 181 182 else if (command.equals("FORWARD")) { 183 digitalWrite(r1, LOW); 184 digitalWrite(r2, HIGH); 185 digitalWrite(l1, LOW); 186 digitalWrite(l2, HIGH); 187 delay(5000); 188 189 } 190 191 else if (command.equals("BACK")) { 192 digitalWrite(r1, HIGH); 193 digitalWrite(r2, LOW); 194 digitalWrite(l1, HIGH); 195 digitalWrite(l2, LOW); 196 delay(5000); 197 } 198 else if (command.equals("LEFT")) { 199 digitalWrite(r1, LOW); 200 digitalWrite(r2, HIGH); 201 digitalWrite(l1, HIGH); 202 digitalWrite(l2, LOW); 203 delay(2000); 204 } 205 else if (command.equals("RIGHT")) { 206 digitalWrite(r1, HIGH); 207 digitalWrite(r2, LOW); 208 digitalWrite(l1, LOW); 209 digitalWrite(l2, HIGH); 210 delay(2000); 211 } 212 213 else if (command.equals("ROW ")) { 214 digitalWrite(r1, LOW); 215 digitalWrite(r2, HIGH); 216 digitalWrite(l1, LOW); 217 digitalWrite(l2, HIGH); 218 delay(500); 219 } 220 else { 221 Serial.println("bad command"); 222 } 223 Serial.print("Command: "); 224 Serial.println(command); 225 } 226 227 228 } 229} 230 231 232
Just getting started, not a pro dont judge
arduino
1#include <TinyGPS++.h> 2#include <SoftwareSerial.h> 3 4created by 5 josef mcinturff Common Creative CC 6 7#include <SPI.h> /// 8 wifi script 9#include <WiFiNINA.h> /// wifi script 10#define 11 wifiConnect 8 /// wifi script 12 13char ssid[] = "YOUR 14 WIFI NAME"; /// wifi script 15char pass[] = "Your PASSWORD"; 16 /// wifi script 17int keyIndex = 0; /// 18 wifi script 19 20int status = WL_IDLE_STATUS; /// wifi script 21 22WiFiServer 23 server(80); /// wifi script 24 25 26 27 28 29 30static 31 const int RXPin = 3 , TXPin = 4; 32static const uint32_t GPSBaud = 9600; 33 34 35 // The TinyGPS++ object of Josef McInturff 36TinyGPSPlus 37 gps; 38 39 // The serial connection to 40 the GPS device 41SoftwareSerial ss(RXPin, TXPin); 42 43 44 char t; // 45 TESTING VARIBLE TO DRIVE ROBOT s stop L left R right B Backwards 46 47 const int l1=10,l2=11,r1=12,r2=13; /// motor driving pins connected to 48 relay 49 50 51 52void setup(){ 53 54 //Initialize serial and wait for port 55 to open: 56 Serial.begin(9600); 57 while (!Serial) { 58 ; // wait for serial 59 port to connect. Needed for native USB port only 60 } 61 62 63 64 /// 65 setup for driving the robot below 66 67 pinMode(r1,OUTPUT); // R1 right motors 68 forward 69 pinMode(r2,OUTPUT); // R2 right motors reverse 70 pinMode(l1,OUTPUT); 71 // L1 left motors forward 72 pinMode(l2,OUTPUT); // L2 left motors reverse 73 74 75 digitalWrite (r1,HIGH); //low activates relays i use set all to high 76 77 digitalWrite (r2,HIGH); 78 digitalWrite (l1,HIGH); 79 digitalWrite (l2,HIGH); 80 81 82 Serial.begin(9600); 83 ss.begin(GPSBaud); 84 85 86 87} 88 89void loop(){ 90 91 92 93 94 95 96 97 98 99 // put your main code here, to run repeatedly: 100 101 102// LOW signal 103 of arduino aktivates relay: 104 105 106 // This sketch displays information every 107 time a new sentence is correctly encoded. 108 while (ss.available() > 0){ 109 gps.encode(ss.read()); 110 111 if (gps.location.isUpdated()){ 112 Serial.print("Latitude= "); 113 Serial.print(gps.location.lat(), 114 6); 115 Serial.print(" Longitude= "); 116 Serial.println(gps.location.lng(), 117 6); 118 } 119 } 120 121// driving instructions below 122 123// char t 124 = 'F'; // TESTING VARIBLE TO DRIVE ROBOT s stop L left R right 125 B Backwards 126 127 128 129if(Serial.available()){ 130 t = Serial.read(); 131 132 } 133 134// R1 right motors forward 135// R2 right motors reverse 136// L1 left 137 motors forward 138// L2 left motors reverse 139 140 141if(t == 'F'){ //move 142 forward(all motors rotate in forward direction) 143 digitalWrite(r1,LOW); 144 digitalWrite(r2,HIGH); 145 146 digitalWrite(l1,LOW); 147 digitalWrite(l2,HIGH); 148 delay(1000); 149 t = 'S'; 150 151 } 152 153else if(t == 'B'){ //move reverse (all motors rotate in reverse 154 direction) 155 digitalWrite(r1,HIGH); 156 digitalWrite(r2,LOW); 157 digitalWrite(l1,HIGH); 158 159 digitalWrite(l2,LOW); 160 delay(1000); 161 t = 'S'; 162 } 163 164else if(t 165 == 'L'){ //turn right (left side motors rotate in forward direction, right 166 side motors rotate in reverse direction for 100ms & stop) 167 digitalWrite(r1,LOW); 168 169 digitalWrite(r2,HIGH); 170 digitalWrite(l1,HIGH); 171 digitalWrite(l2,LOW); 172 173 delay(1000); 174 t = 'S'; 175 } 176 177else if(t == 'R'){ //turn left 178 (right side motors rotate in forward direction, left side motors rotate in reverse 179 direction for 100ms & stop) 180 digitalWrite(r1,HIGH); 181 digitalWrite(r2,LOW); 182 183 digitalWrite(l1,LOW); 184 digitalWrite(l2,HIGH); 185 delay(1000); 186 t = 'S'; 187 188 } 189 190else if(t == 'S'){ //STOP (all motors stop) 191 digitalWrite(r1,HIGH); 192 193 digitalWrite(r2,HIGH); 194 digitalWrite(l1,HIGH); 195 digitalWrite(l2,HIGH); 196 197 } 198delay(100); 199 200 201 202} 203
Just getting started, not a pro dont judge
arduino
testing these individual parts as we progress
1#include <LiquidCrystal.h> 2#include <SoftwareSerial.h> 3#include 4 <TinyGPS.h> 5#include <Math.h> 6 7 8 9 10 11 12 char t; // 13 TESTING VARIBLE TO DRIVE ROBOT s stop L left R right B Backwards 14 15 const int l1=10,l2=11,r1=12,r2=13; /// motor driving pins connected to 16 relay 17 18 19 // written by Josef 20 McInturff 21 22 //long lat,lon; 23 // create variable for latitude and longitude object 24 float lat = 28.54,lon 25 = 77.17; // create variable for latitude and longitude object 26 SoftwareSerial 27 gpsSerial(4,3); //rx,tx 28 LiquidCrystal lcd(A0,A1,A2,A3,A4,A5); ///LCD 29 PINS LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 30 TinyGPS gps; // 31 create gps object 32 33 34 35 36 37void setup() { 38 39 40 Serial.begin(9600); 41 42 43 //Serial.println("The GPS Received 44 Signal:"); 45 gpsSerial.begin(9600); // connect gps sensor 46 47 lcd.begin(16,2); 48 49 50 51 52 53 54 55 56 57 58 59 60 /// 61 setup for driving the robot below 62 63 pinMode(r1,OUTPUT); //right motors 64 forward 65 pinMode(r2,OUTPUT); //right motors reverse 66 pinMode(l1,OUTPUT); 67 //left motors forward 68 pinMode(l2,OUTPUT); //left motors reverse 69 70 71 digitalWrite (r1,HIGH); //low activates relays i use set all to high 72 73 digitalWrite (r2,HIGH); 74 digitalWrite (l1,HIGH); 75 digitalWrite (l2,HIGH); 76 77 78} 79 80 81void loop() { 82 83 84 85///setting up GPS On LCD display 86 87 88 89 while(gpsSerial.available()){ // check for gps data 90 if(gps.encode(gpsSerial.read()))// 91 encode gps data 92 93 gps.f_get_position(&lat,&lon); // get latitude and 94 longitude 95 // display position 96 lcd.clear(); 97 lcd.setCursor(1,0); 98 99 lcd.print("GPS Signal"); 100 //Serial.print("Position: "); 101 //Serial.print("Latitude:"); 102 103 //Serial.print(lat,6); 104 //Serial.print(";"); 105 //Serial.print("Longitude:"); 106 107 //Serial.println(lon,6); 108 lcd.setCursor(1,0); 109 lcd.print("LAT:"); 110 111 lcd.setCursor(5,0); 112 lcd.print(lat); 113 //Serial.print(lat); 114 115 //Serial.print(" "); 116 117 lcd.setCursor(0,1); 118 lcd.print(",LON:"); 119 120 lcd.setCursor(5,1); 121 lcd.print(lon); 122} 123 124 125 126 String 127 latitude = String(lat,6); 128 String longitude = String(lon,6); 129 Serial.println(latitude+";"+longitude); 130 131 delay(1000); 132 133 134 135 136 137 138 139// driving instructions below 140 141char 142 t = 'R'; // TESTING VARIBLE TO DRIVE ROBOT s stop L left R right 143 B Backwards 144 145 146 147if(Serial.available()){ 148 t = Serial.read(); 149 150 } 151 152if(t == 'F'){ //move forward(all motors rotate in forward 153 direction) 154 digitalWrite(r1,LOW); 155 digitalWrite(r2,HIGH); 156 digitalWrite(l1,LOW); 157 158 digitalWrite(l2,HIGH); 159 delay(1000); 160 t = 'S'; 161 } 162 163else if(t 164 == 'B'){ //move reverse (all motors rotate in reverse direction) 165 digitalWrite(r1,HIGH); 166 167 digitalWrite(r2,LOW); 168 digitalWrite(l1,HIGH); 169 digitalWrite(l2,LOW); 170 171 delay(1000); 172 t = 'S'; 173 } 174 175else if(t == 'L'){ //turn right (left 176 side motors rotate in forward direction, right side motors rotate in reverse direction 177 for 100ms & stop) 178 digitalWrite(r1,LOW); 179 digitalWrite(r2,HIGH); 180 digitalWrite(l1,HIGH); 181 182 digitalWrite(l2,LOW); 183 delay(1000); 184 t = 'S'; 185 } 186 187else if(t 188 == 'R'){ //turn left (right side motors rotate in forward direction, left side 189 motors rotate in reverse direction for 100ms & stop) 190 digitalWrite(r1,HIGH); 191 192 digitalWrite(r2,LOW); 193 digitalWrite(l1,LOW); 194 digitalWrite(l2,HIGH); 195 196 delay(1000); 197 t = 'S'; 198 } 199 200else if(t == 'S'){ //STOP (all 201 motors stop) 202 digitalWrite(r1,HIGH); 203 digitalWrite(r2,HIGH); 204 digitalWrite(l1,HIGH); 205 206 digitalWrite(l2,HIGH); 207 } 208delay(100); 209 210 } 211
Drivable robot on command line storing sampler data on SD card with GPS coordinates
arduino
drive robot to desired location push button then record sensor readings and GPS data store to SD card
1//// pin layout digital side 0 RX GPS 2// 1 3 tx GPS 4// 2 driving left motor forward 5// 6 3 driving Left motor reverse (make sure they are 7 mechanically interlocked so both dont com on at once) Blinking on relay during startup 8 or data x fer can short out load circuit motor drives 9// 4 10 ** SD card shield madatory pin 11// 5 driving 12 Right side Forward 13// 6 driving Right side Backwards 14 (same note as left motor protect reverse polarity shorts) 15// 7 16// 17 8 18// 9 dispense 19 water pin on 20// 10 dispense nutrients pin 21// 22 11 **** MOSI - pin 11 23// 12 24 ** MISO - pin 12 25// 13 ** CLK - pin 26 13 27 /// pin 4 is required to be used 28 by my SD card shield 29 // ** MOSI - 30 pin 11 31 // ** MISO - pin 12 32 // 33 ** CLK - pin 13 34// analog side 0 35// 1 36// 37 2 38// 3 39// 40 4 41// 5 42#include 43 <TinyGPS++.h> 44#include <SoftwareSerial.h> 45 46#include <SPI.h> /// 47 sd card reader 48#include <SD.h> /// sd card reader 49 50#include 51 <SPI.h> /// wifi script 52#include <WiFiNINA.h> /// 53 wifi script 54#define wifiConnect 8 /// wifi script 55 56char 57 ssid[] = "wifi name"; /// wifi script 58char pass[] = "Password 59 wifi"; /// wifi script 60int keyIndex = 0; /// 61 wifi script 62 63int status = WL_IDLE_STATUS; /// wifi script 64 65WiFiServer 66 server(80); /// wifi script 67 68int toggleState; ///soil 69 sampler code 70int buttonStatus = 1; ///soil sampler 71 code 72 73 74 75String command; ///part of beeing 76 able to send commands from terminal line to drive 77 78 79 80 81 82static 83 const int RXPin = 0 , TXPin = 1; 84static const uint32_t GPSBaud = 9600; 85 86 87 // The TinyGPS++ object of Josef McInturff 88TinyGPSPlus 89 gps; 90 91 // The serial connection to 92 the GPS device 93SoftwareSerial ss(RXPin, TXPin); 94 95 96 const int l1=2,l2=3,r1=5,r2=6;// 97 boomleft=7, boomright=8; /// motor driving pins connected to relay 98 99/// 100 pin 4 is required to be used by my SD card shield 101// ** MOSI - pin 11 102// 103 ** MISO - pin 12 104// ** CLK - pin 13 105// ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN) 106const 107 int chipSelect = 4; 108 109void setup(){ 110 111 pinMode(wifiConnect, OUTPUT); 112 /// wifi script 113 114 115 //Initialize serial and wait for port 116 to open: 117 Serial.begin(9600); 118 while (!Serial) { 119 ; // wait for serial 120 port to connect. Needed for native USB port only 121 } 122Serial.print("Initializing 123 SD card..."); //// SD Card script 124 if (!SD.begin(chipSelect)) 125 { 126 Serial.println("Card failed, or not present! may need to be rebooted"); 127 128 129 // don't do anything more: 130 while (1); 131 } 132 Serial.println("card 133 initialized."); 134 135 136 137 // initialize serial communication at 138 9600 bits per second: 139 140 Serial.begin(9600); 141 pinMode(9, 142 OUTPUT); // turn water relay on "needs water" ///soil 143 sampler code 144 pinMode(10, OUTPUT); // turn water relay on "needs nutrients" 145 ///soil 146 sampler code 147 // this sketch also uses Analog pin 148 1 water sensing sensor value over 1000 under 900 ///soil 149 sampler code 150 // this sketch also uses Analog pin 151 2 nutrients sensing ///soil 152 sampler code 153 pinMode(8, INPUT_PULLUP); // enable internal pull-up pin 154 8 will be used as trigger by the {gps target plant reached} pin Later pullup may 155 need to bemoved later ///soil 156 sampler code 157 158 // pin 8 is currently a push botton but may later 159 be set by "target arrived" function of my gps i also want to use this pin to 160 take instant reading where the head unit is currently ///soil 161 sampler code 162 163 Serial.begin(9600); 164 Serial.println("please 165 push botton to trigger sensor reading"); ///soil 166 sampler code 167 ///soil sampler code 168 169 170 171 172 173 174 175 176 177 178 179 180 181 /// setup for driving the robot below 182 183 184 pinMode(r1,OUTPUT); // R1 right motors forward 185 pinMode(r2,OUTPUT); // 186 R2 right motors reverse 187 pinMode(l1,OUTPUT); // L1 left motors forward 188 189 pinMode(l2,OUTPUT); // L2 left motors reverse 190 191 digitalWrite (r1,HIGH); 192 //low activates relays i use set all to high 193 digitalWrite (r2,HIGH); 194 195 digitalWrite (l1,HIGH); 196 digitalWrite (l2,HIGH); 197 198 Serial.begin(9600); 199 200 ss.begin(GPSBaud); 201 202while (status != WL_CONNECTED) { /// wifi 203 script 204 Serial.print("Attempting to connect to Network named: "); /// 205 wifi script 206 Serial.println(ssid); /// wifi script 207 status 208 = WiFi.begin(ssid, pass); /// wifi script 209 delay(10000); /// 210 wifi script 211 } /// wifi script 212 213 server.begin(); /// 214 wifi script 215 216 digitalWrite(wifiConnect, HIGH); /// wifi script 217 218 219 // Serial.print("SSID: "); /// wifi script 220 // Serial.println(WiFi.SSID()); 221 /// wifi script 222 IPAddress ip = WiFi.localIP(); /// 223 wifi script 224 Serial.print("IP Address: "); /// wifi script 225 226 Serial.println(ip); /// wifi script 227 228} 229 230void loop(){ 231 232 String 233 dataString = ""; // sd card script 234 235 236 // This sketch displays information every time a new sentence is correctly encoded. 237 238 while (ss.available() > 0){ 239 gps.encode(ss.read()); 240 if (gps.location.isUpdated()){ 241 242 Serial.print("Latitude= "); 243 Serial.print(gps.location.lat(), 6); 244 245 Serial.print(" Longitude= "); 246 Serial.println(gps.location.lng(), 247 6); 248 249 Serial.print("SSID: "); /// wifi script 250 Serial.println(WiFi.SSID()); 251 /// wifi script 252 IPAddress ip = WiFi.localIP(); /// 253 wifi script 254 Serial.print("IP Address: "); /// wifi script 255 256 Serial.println(ip); /// wifi script 257 Serial.println(dataString); 258 // print sensor to the serial port too: 259 260 261 } 262 } 263 264 265 266 267 268 269 270 271 272 // Set LED colour to their Digital Ouputs. Read the input on analog pin 0: ///soil 273 sampler code 274 int water = 9; //feeding pin to run pump 275 for water ///soil sampler code 276 int nutrients 277 = 10; // feeding pin for nutrients ///soil 278 sampler code 279 int sensorValueWater = analogRead(A4); ///soil 280 sampler code 281 int sensorValueNutrients = analogRead(A5); ///soil 282 sampler code 283 284 int pinValue = digitalRead(8); ///Pushbotton 285 code ///soil sampler code 286 delay(10); // 287 quick and dirty debounce filter ///soil 288 sampler code 289 if (buttonStatus != pinValue) { ///Pushbotton 290 code ///soil sampler code 291 buttonStatus 292 = pinValue; ///Pushbotton code ///soil 293 sampler code 294 Serial.println(buttonStatus); ///Pushbotton 295 code ///soil sampler code 296 } ///soil 297 sampler code 298 // Set the initial state of the LEDs to OFF ///soil 299 sampler code 300 digitalWrite(9, HIGH); //feeding pin to run pump for water 301 Low activates relay ///soil 302 sampler code 303 digitalWrite(10, HIGH); // feeding pin for nutrients low 304 activated relay ///soil sampler code 305 306 307 308 309if(buttonStatus 310 > 0) { //// in this loop we take readings and set relay 311 to water and nutrients dispensary ///soil 312 sampler code 313 ///soil 314 sampler code 315 // Logic Loop that sets the required 316 LED to ON 317 if (sensorValueWater >= 1000) (digitalWrite(water, 318 LOW)); ////watering code 319 320 else if ((sensorValueWater <= 999) && (sensorValueWater 321 >=901)) (digitalWrite(water, HIGH)); ////watering code 322 323 else if (sensorValueWater <= 900) (digitalWrite(water, 324 HIGH)); ////watering code 325 326 else ; 327 328 329 // 330 Prints the condition of soil. Dry, Wet or Perfect 331 if 332 (sensorValueWater >= 1000) (Serial.print("SOIL IS TOO DRY!!!!! watering now ")); 333 ////watering code 334 else 335 if ((sensorValueWater<= 999) && (sensorValueWater >=901)) (Serial.print("SOIL IS 336 PERFECT!!!!! ")); ////watering code 337 else 338 if (sensorValueWater <= 900) (Serial.print("SOIL IS TOO WET!!!!! ")); ////watering 339 code 340 else; 341 342 343 // 344 print out the value you read: 345 Serial.print("Soil 346 Humidity: "); 347 Serial.println(sensorValueWater); 348 349 delay(1500); // 350 delay in between reads for stability and keep water and nutrients on for this delay 351 352 digitalWrite(9, HIGH); //feeding pin to run 353 pump for water 354 355 356 357 // Logic Loop that 358 sets the required LED to ON 359 if (sensorValueNutrients 360 >= 1000) (digitalWrite(nutrients, LOW)); ////nutirents 361 code 362 else if ((sensorValueNutrients <= 999) && 363 (sensorValueNutrients >=901)) (digitalWrite(nutrients, HIGH)); ////nutrients 364 code 365 else if (sensorValueNutrients <= 900) (digitalWrite(nutrients, 366 HIGH)); ////nutrients code 367 368 else ; 369 370 371 // 372 Prints the condition of soil. Dry, Wet or Perfect 373 if 374 (sensorValueNutrients >= 1000) (Serial.print("nutrients are LOW dispensing now 375 ")); ////watering code 376 else 377 if ((sensorValueNutrients<= 999) && (sensorValueNutrients >=901)) (Serial.print("SOIL 378 IS PERFECT!!!!! ")); ////watering code 379 else 380 if (sensorValueNutrients <= 900) (Serial.print("SOIL IS TOO HOT IN NUTRIENTS!!! 381 ")); ////watering code 382 else; 383 384 385 386 387 // print out 388 the value you read: 389 Serial.print("Nutrients: "); 390 391 Serial.println(sensorValueNutrients); 392 delay(1500); 393 // delay in between reads 394 for stability and keep water and nutrients on for this delay 395 digitalWrite(10, 396 HIGH); // feeding pin for nutrients 397 398 399 400 // 401 read GPS location and append to the string: 402 for (int analogPin = 0; analogPin 403 < 3; analogPin++) { // sd card script takes value from input 0 analog on 404 first go thru loop 405 int sensor = analogRead(analogPin); // 406 sd card script 407 dataString += String(sensor); // 408 sd card script 409 if (analogPin < 2) { // 410 sd card script 411 dataString += ",";} // 412 sd card script 413 if (analogPin >= 2) { 414 dataString += ","; 415 416 dataString += "Latitude"; 417 dataString += ","; 418 dataString 419 += String (gps.location.lat(), 6); /// dataString += (gps.location.lat(), 6); 420 421 dataString += ","; 422 dataString += "Longitude"; 423 dataString 424 += ","; 425 dataString += String (gps.location.lng(), 6); 426 dataString 427 += ","; 428 dataString += "Watersensor"; 429 dataString += ","; 430 431 dataString += String (sensorValueWater); 432 dataString += ","; 433 434 dataString += "Nutrientsensor"; 435 dataString += ","; 436 dataString 437 += String (sensorValueNutrients); 438 dataString += ","; 439 440// open the 441 file. note that only one file can be open at a time, 442 // so you have to close 443 this one before opening another. 444File dataFile = SD.open("datalog.txt", FILE_WRITE); 445 446 447 // if the file is available, write to it: 448 if (dataFile) { 449 dataFile.println(dataString); 450 451 dataFile.close(); 452 } 453 // if the file isn't open, pop up an error: 454 455 else { 456 Serial.println("error opening datalog.txt Replace SD card reboot"); 457 458 } 459 } 460delay(200); 461 462 } 463 464 } 465 466 467 468 469 while(buttonStatus >= 1) { // this code makes 470 sure we are only running samples once after pushing putton nat as long as button 471 is pushed manually (gps location arrived i dnt want to run this sketch non stop) 472 473 toggleState=! toggleState; 474 digitalWrite(8, 475 toggleState); 476 delay(200) ; 477 buttonStatus 478 = digitalRead (8); 479 } 480 toggleState =! toggleState; 481 482 digitalWrite (8, toggleState); 483 delay(200); 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 // open the file. note that only one file can be open at a time, 527 528 // so you have to close this one before opening another. 529// File 530 dataFile = SD.open("datalog.txt", FILE_WRITE); 531 532 // if the 533 file is available, write to it: 534// if (dataFile) { 535// dataFile.println(dataString); 536// 537 dataFile.close(); 538 // } 539 // 540 if the file isn't open, pop up an error: 541// else { 542 // Serial.println("error 543 opening datalog.txt Replace SD card reboot"); 544 // } 545 546 // 547 put your main code here, to run repeatedly: 548 549 550 // LOW signal 551 of arduino aktivates relay: 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574// 575 driving instructions below 576if(Serial.available()){ 577} 578 579 if (Serial.available()) 580 { 581 command = Serial.readStringUntil('\ 582'); 583 command.trim(); 584 if 585 (command.equals("STOP")) { 586 digitalWrite(r1, HIGH); 587 digitalWrite(r2, 588 HIGH); 589 digitalWrite(l1, HIGH); 590 digitalWrite(l2, HIGH); 591 delay(1000); 592 593 } 594 595 else if (command.equals("FORWARD")) { 596 digitalWrite(r1, 597 LOW); 598 digitalWrite(r2, HIGH); 599 digitalWrite(l1, LOW); 600 digitalWrite(l2, 601 HIGH); 602 delay(5000); 603 604 } 605 606 else if (command.equals("BACK")) 607 { 608 digitalWrite(r1, HIGH); 609 digitalWrite(r2, LOW); 610 digitalWrite(l1, 611 HIGH); 612 digitalWrite(l2, LOW); 613 delay(5000); 614 615 } 616 else if (command.equals("LEFT")) { 617 digitalWrite(r1, LOW); 618 619 digitalWrite(r2, HIGH); 620 digitalWrite(l1, HIGH); 621 digitalWrite(l2, 622 LOW); 623 delay(2000); 624 } 625 else if (command.equals("RIGHT")) 626 { 627 digitalWrite(r1, HIGH); 628 digitalWrite(r2, LOW); 629 digitalWrite(l1, 630 LOW); 631 digitalWrite(l2, HIGH); 632 delay(2000); 633 } 634 635 636 else if (command.equals("ROW ")) { 637 digitalWrite(r1, LOW); 638 digitalWrite(r2, 639 HIGH); 640 digitalWrite(l1, LOW); 641 digitalWrite(l2, HIGH); 642 643 delay(500); 644 } 645 else { 646 Serial.println("bad command"); 647 648 } 649 Serial.print("Command: "); 650 Serial.println(command); 651 } 652 653 654 655 656} 657 658 659 660 661////credits to : tutorials from 662 663///https://www.elithecomputerguy.com/author/admin/ 664///Paul 665 McWhorter his youtube video shows step-by-step instructions
Soil sampler code
arduino
in this sketch we push a button to display current soil readings
1int toggleState; 2int buttonStatus = 0; 3 4 5void setup() { 6 // initialize serial communication at 9600 bits per second: 7 8 Serial.begin(9600); 9 pinMode(9, OUTPUT); // turn water relay on "needs water" 10 pinMode(10, OUTPUT); // turn water relay on "needs nutrients" 11 // this sketch also uses Analog pin 1 water sensing sensor value over 1000 under 900 12 // this sketch also uses Analog pin 2 nutrients sensing 13 pinMode(8, INPUT_PULLUP); // enable internal pull-up pin 8 will be used as trigger by the {gps target plant reached} pin Later pullup may need to bemoved later 14 15 // pin 8 is currently a push botton but may later be set by "target arrived" function of my gps i also want to use this pin to take instant reading where the head unit is currently 16 17 Serial.begin(9600); 18 Serial.println("please push botton to trigger sensor reading"); 19} 20 21 22 23// the loop routine runs over and over again forever: 24void loop() { 25 26 // Set LED colour to their Digital Ouputs. Read the input on analog pin 0: 27 int water = 9; //feeding pin to run pump for water 28 int nutrients = 10; // feeding pin for nutrients 29 int sensorValueWater = analogRead(A1); 30 int sensorValueNutrients = analogRead(A2); 31 32 int pinValue = digitalRead(8); ///Pushbotton code 33 delay(10); // quick and dirty debounce filter 34 if (buttonStatus != pinValue) { ///Pushbotton code 35 buttonStatus = pinValue; ///Pushbotton code 36 Serial.println(buttonStatus); ///Pushbotton code 37 } 38 // Set the initial state of the LEDs to OFF 39 digitalWrite(9, HIGH); //feeding pin to run pump for water Low activates relay 40 digitalWrite(10, HIGH); // feeding pin for nutrients low activated relay 41 42 43 44 45if(buttonStatus > 0) { //// in this loop we take readings and set relay to water and nutrients dispensary 46 47 // Logic Loop that sets the required LED to ON 48 if (sensorValueWater >= 1000) (digitalWrite(water, LOW)); ////watering code 49 else if ((sensorValueWater <= 999) && (sensorValueWater >=901)) (digitalWrite(water, HIGH)); ////watering code 50 else if (sensorValueWater <= 900) (digitalWrite(water, HIGH)); ////watering code 51 else ; 52 53 54 // Prints the condition of soil. Dry, Wet or Perfect 55 if (sensorValueWater >= 1000) (Serial.print("SOIL IS TOO DRY!!!!! watering now ")); ////watering code 56 else if ((sensorValueWater<= 999) && (sensorValueWater >=901)) (Serial.print("SOIL IS PERFECT!!!!! ")); ////watering code 57 else if (sensorValueWater <= 900) (Serial.print("SOIL IS TOO WET!!!!! ")); ////watering code 58 else; 59 60 61 // print out the value you read: 62 Serial.print("Soil Humidity: "); 63 Serial.println(sensorValueWater); 64 delay(1500); // delay in between reads for stability and keep water and nutrients on for this delay 65 digitalWrite(9, HIGH); //feeding pin to run pump for water 66 67 68 69 // Logic Loop that sets the required LED to ON 70 if (sensorValueNutrients >= 1000) (digitalWrite(nutrients, LOW)); ////nutirents code 71 else if ((sensorValueNutrients <= 999) && (sensorValueNutrients >=901)) (digitalWrite(nutrients, HIGH)); ////nutrients code 72 else if (sensorValueNutrients <= 900) (digitalWrite(nutrients, HIGH)); ////nutrients code 73 else ; 74 75 76 // Prints the condition of soil. Dry, Wet or Perfect 77 if (sensorValueNutrients >= 1000) (Serial.print("nutrients are LOW dispensing now ")); ////watering code 78 else if ((sensorValueNutrients<= 999) && (sensorValueNutrients >=901)) (Serial.print("SOIL IS PERFECT!!!!! ")); ////watering code 79 else if (sensorValueNutrients <= 900) (Serial.print("SOIL IS TOO HOT IN NUTRIENTS!!! ")); ////watering code 80 else; 81 82 83 84 // print out the value you read: 85 Serial.print("Nutrients: "); 86 Serial.println(sensorValueNutrients); 87 delay(1500); // delay in between reads for stability and keep water and nutrients on for this delay 88 digitalWrite(10, HIGH); // feeding pin for nutrients 89 90 91 92 } 93 94 95 96 while(buttonStatus >= 1) { // this code makes sure we are only running samples once after pushing putton nat as long as button is pushed manually (gps location arrived i dnt want to run this sketch non stop) 97 toggleState=! toggleState; 98 digitalWrite(8, toggleState); 99 delay(200) ; 100 buttonStatus = digitalRead (8); 101 } 102 toggleState =! toggleState; 103 digitalWrite (8, toggleState); 104 delay(200); 105 106 107 108 109}
Just getting started, not a pro dont judge
arduino
now with data logger for each entry onto cvs file GPS is also recorded
1#include <TinyGPS++.h> 2#include <SoftwareSerial.h> 3 4#include <SPI.h> /// sd card reader 5#include <SD.h> /// sd card reader 6 7#include <SPI.h> /// wifi script 8#include <WiFiNINA.h> /// wifi script 9#define wifiConnect 8 /// wifi script 10 11char ssid[] = "wifiname"; /// wifi script case sensitive 12char pass[] = "wifi passw"; /// wifi script case sensitive 13int keyIndex = 0; /// wifi script 14 15int status = WL_IDLE_STATUS; /// wifi script 16 17WiFiServer server(80); /// wifi script 18 19String command; ///part of beeing able to send commands from terminal line to drive 20 21 22 23 24 25static const int RXPin = 0 , TXPin = 1; 26static const uint32_t GPSBaud = 9600; 27 28 // The TinyGPS++ object of Josef McInturff 29TinyGPSPlus gps; 30 31 // The serial connection to the GPS device 32SoftwareSerial ss(RXPin, TXPin); 33 34 35 const int l1=2,l2=3,r1=5,r2=6; /// motor driving pins connected to relay 36 37/// pin 4 is required to be used by my SD card shield 38// ** MOSI - pin 11 39// ** MISO - pin 12 40// ** CLK - pin 13 41// ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN) 42const int chipSelect = 4; 43 44void setup(){ 45 46 pinMode(wifiConnect, OUTPUT); /// wifi script 47 48 49 //Initialize serial and wait for port to open: 50 Serial.begin(9600); 51 while (!Serial) { 52 ; // wait for serial port to connect. Needed for native USB port only 53 } 54Serial.print("Initializing SD card..."); //// SD Card script 55 if (!SD.begin(chipSelect)) { 56 Serial.println("Card failed, or not present"); 57 // don't do anything more: 58 while (1); 59 } 60 Serial.println("card initialized."); 61 62 /// setup for driving the robot below 63 64 pinMode(r1,OUTPUT); // R1 right motors forward 65 pinMode(r2,OUTPUT); // R2 right motors reverse 66 pinMode(l1,OUTPUT); // L1 left motors forward 67 pinMode(l2,OUTPUT); // L2 left motors reverse 68 69 digitalWrite (r1,HIGH); //low activates relays i use set all to high 70 digitalWrite (r2,HIGH); 71 digitalWrite (l1,HIGH); 72 digitalWrite (l2,HIGH); 73 74 Serial.begin(9600); 75 ss.begin(GPSBaud); 76 77while (status != WL_CONNECTED) { /// wifi script 78 Serial.print("Attempting to connect to Network named: "); /// wifi script 79 Serial.println(ssid); /// wifi script 80 status = WiFi.begin(ssid, pass); /// wifi script 81 delay(10000); /// wifi script 82 } /// wifi script 83 84 server.begin(); /// wifi script 85 86 digitalWrite(wifiConnect, HIGH); /// wifi script 87 88 // Serial.print("SSID: "); /// wifi script 89 // Serial.println(WiFi.SSID()); /// wifi script 90 IPAddress ip = WiFi.localIP(); /// wifi script 91 Serial.print("IP Address: "); /// wifi script 92 Serial.println(ip); /// wifi script 93 94} 95 96void loop(){ 97 98 String dataString = ""; // sd card script 99 100 // This sketch displays information every time a new sentence is correctly encoded. 101 while (ss.available() > 0){ 102 gps.encode(ss.read()); 103 if (gps.location.isUpdated()){ 104 Serial.print("Latitude= "); 105 Serial.print(gps.location.lat(), 6); 106 Serial.print(" Longitude= "); 107 Serial.println(gps.location.lng(), 6); 108 109 Serial.print("SSID: "); /// wifi script 110 Serial.println(WiFi.SSID()); /// wifi script 111 IPAddress ip = WiFi.localIP(); /// wifi script 112 Serial.print("IP Address: "); /// wifi script 113 Serial.println(ip); /// wifi script 114 Serial.println(dataString); // print sensor to the serial port too: 115 116 117 } 118 } 119//Serial.print("Type Command ( STOP, FORWARD, BACK, LEFT, RIGHT )"); /// sending commands via terminal 120 121 122 123 // read three sensors and append to the string: 124 for (int analogPin = 0; analogPin < 3; analogPin++) { // sd card script takes value from input 0 analog on first go thru loop 125 int sensor = analogRead(analogPin); // sd card script 126 dataString += String(sensor); // sd card script 127 if (analogPin < 2) { // sd card script 128 dataString += ",";} // sd card script 129 if (analogPin >= 2) { 130 dataString += ","; 131 dataString += "Latitude"; 132 dataString += ","; 133 dataString += String (gps.location.lat(), 6); /// dataString += (gps.location.lat(), 6); 134 dataString += ","; 135 dataString += "Longitude"; 136 dataString += ","; 137 dataString += String (gps.location.lng(), 6); 138 dataString += ","; 139 } 140 } 141 142// open the file. note that only one file can be open at a time, 143 // so you have to close this one before opening another. 144File dataFile = SD.open("datalog.txt", FILE_WRITE); 145 146 // if the file is available, write to it: 147 if (dataFile) { 148 dataFile.println(dataString); 149 dataFile.close(); 150 } 151 // if the file isn't open, pop up an error: 152 else { 153 Serial.println("error opening datalog.txt Replace SD card reboot"); 154 } 155 156 // put your main code here, to run repeatedly: 157 158 159// LOW signal of arduino aktivates relay: 160 161 162 163 164 165 166 167 168 169 170 171 172 173// driving instructions below 174if(Serial.available()){ 175} 176 177 if (Serial.available()) { 178 command = Serial.readStringUntil('\n'); 179 command.trim(); 180 if (command.equals("STOP")) { 181 digitalWrite(r1, HIGH); 182 digitalWrite(r2, HIGH); 183 digitalWrite(l1, HIGH); 184 digitalWrite(l2, HIGH); 185 delay(1000); 186 } 187 188 else if (command.equals("FORWARD")) { 189 digitalWrite(r1, LOW); 190 digitalWrite(r2, HIGH); 191 digitalWrite(l1, LOW); 192 digitalWrite(l2, HIGH); 193 delay(5000); 194 195 } 196 197 else if (command.equals("BACK")) { 198 digitalWrite(r1, HIGH); 199 digitalWrite(r2, LOW); 200 digitalWrite(l1, HIGH); 201 digitalWrite(l2, LOW); 202 delay(5000); 203 } 204 else if (command.equals("LEFT")) { 205 digitalWrite(r1, LOW); 206 digitalWrite(r2, HIGH); 207 digitalWrite(l1, HIGH); 208 digitalWrite(l2, LOW); 209 delay(2000); 210 } 211 else if (command.equals("RIGHT")) { 212 digitalWrite(r1, HIGH); 213 digitalWrite(r2, LOW); 214 digitalWrite(l1, LOW); 215 digitalWrite(l2, HIGH); 216 delay(2000); 217 } 218 219 else if (command.equals("ROW ")) { 220 digitalWrite(r1, LOW); 221 digitalWrite(r2, HIGH); 222 digitalWrite(l1, LOW); 223 digitalWrite(l2, HIGH); 224 delay(500); 225 } 226 else { 227 Serial.println("bad command"); 228 } 229 Serial.print("Command: "); 230 Serial.println(command); 231 } 232 233 234 235} 236 237 238 239 240////credits to : tutorials from 241 242///https://www.elithecomputerguy.com/author/admin/ 243///Paul McWhorter his youtube video shows step-by-step instructions 244
Drivable robot on command line storing sampler data on SD card with GPS coordinates
arduino
drive robot to desired location push button then record sensor readings and GPS data store to SD card
1//// pin layout digital side 0 RX GPS 2// 1 tx GPS 3// 2 driving left motor forward 4// 3 driving Left motor reverse (make sure they are mechanically interlocked so both dont com on at once) Blinking on relay during startup or data x fer can short out load circuit motor drives 5// 4 ** SD card shield madatory pin 6// 5 driving Right side Forward 7// 6 driving Right side Backwards (same note as left motor protect reverse polarity shorts) 8// 7 9// 8 10// 9 dispense water pin on 11// 10 dispense nutrients pin 12// 11 **** MOSI - pin 11 13// 12 ** MISO - pin 12 14// 13 ** CLK - pin 13 15 /// pin 4 is required to be used by my SD card shield 16 // ** MOSI - pin 11 17 // ** MISO - pin 12 18 // ** CLK - pin 13 19// analog side 0 20// 1 21// 2 22// 3 23// 4 24// 5 25#include <TinyGPS++.h> 26#include <SoftwareSerial.h> 27 28#include <SPI.h> /// sd card reader 29#include <SD.h> /// sd card reader 30 31#include <SPI.h> /// wifi script 32#include <WiFiNINA.h> /// wifi script 33#define wifiConnect 8 /// wifi script 34 35char ssid[] = "wifi name"; /// wifi script 36char pass[] = "Password wifi"; /// wifi script 37int keyIndex = 0; /// wifi script 38 39int status = WL_IDLE_STATUS; /// wifi script 40 41WiFiServer server(80); /// wifi script 42 43int toggleState; ///soil sampler code 44int buttonStatus = 1; ///soil sampler code 45 46 47 48String command; ///part of beeing able to send commands from terminal line to drive 49 50 51 52 53 54static const int RXPin = 0 , TXPin = 1; 55static const uint32_t GPSBaud = 9600; 56 57 // The TinyGPS++ object of Josef McInturff 58TinyGPSPlus gps; 59 60 // The serial connection to the GPS device 61SoftwareSerial ss(RXPin, TXPin); 62 63 64 const int l1=2,l2=3,r1=5,r2=6;// boomleft=7, boomright=8; /// motor driving pins connected to relay 65 66/// pin 4 is required to be used by my SD card shield 67// ** MOSI - pin 11 68// ** MISO - pin 12 69// ** CLK - pin 13 70// ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN) 71const int chipSelect = 4; 72 73void setup(){ 74 75 pinMode(wifiConnect, OUTPUT); /// wifi script 76 77 78 //Initialize serial and wait for port to open: 79 Serial.begin(9600); 80 while (!Serial) { 81 ; // wait for serial port to connect. Needed for native USB port only 82 } 83Serial.print("Initializing SD card..."); //// SD Card script 84 if (!SD.begin(chipSelect)) { 85 Serial.println("Card failed, or not present! may need to be rebooted"); 86 87 // don't do anything more: 88 while (1); 89 } 90 Serial.println("card initialized."); 91 92 93 94 // initialize serial communication at 9600 bits per second: 95 96 Serial.begin(9600); 97 pinMode(9, OUTPUT); // turn water relay on "needs water" ///soil sampler code 98 pinMode(10, OUTPUT); // turn water relay on "needs nutrients" ///soil sampler code 99 // this sketch also uses Analog pin 1 water sensing sensor value over 1000 under 900 ///soil sampler code 100 // this sketch also uses Analog pin 2 nutrients sensing ///soil sampler code 101 pinMode(8, INPUT_PULLUP); // enable internal pull-up pin 8 will be used as trigger by the {gps target plant reached} pin Later pullup may need to bemoved later ///soil sampler code 102 103 // pin 8 is currently a push botton but may later be set by "target arrived" function of my gps i also want to use this pin to take instant reading where the head unit is currently ///soil sampler code 104 105 Serial.begin(9600); 106 Serial.println("please push botton to trigger sensor reading"); ///soil sampler code 107 ///soil sampler code 108 109 110 111 112 113 114 115 116 117 118 119 120 /// setup for driving the robot below 121 122 pinMode(r1,OUTPUT); // R1 right motors forward 123 pinMode(r2,OUTPUT); // R2 right motors reverse 124 pinMode(l1,OUTPUT); // L1 left motors forward 125 pinMode(l2,OUTPUT); // L2 left motors reverse 126 127 digitalWrite (r1,HIGH); //low activates relays i use set all to high 128 digitalWrite (r2,HIGH); 129 digitalWrite (l1,HIGH); 130 digitalWrite (l2,HIGH); 131 132 Serial.begin(9600); 133 ss.begin(GPSBaud); 134 135while (status != WL_CONNECTED) { /// wifi script 136 Serial.print("Attempting to connect to Network named: "); /// wifi script 137 Serial.println(ssid); /// wifi script 138 status = WiFi.begin(ssid, pass); /// wifi script 139 delay(10000); /// wifi script 140 } /// wifi script 141 142 server.begin(); /// wifi script 143 144 digitalWrite(wifiConnect, HIGH); /// wifi script 145 146 // Serial.print("SSID: "); /// wifi script 147 // Serial.println(WiFi.SSID()); /// wifi script 148 IPAddress ip = WiFi.localIP(); /// wifi script 149 Serial.print("IP Address: "); /// wifi script 150 Serial.println(ip); /// wifi script 151 152} 153 154void loop(){ 155 156 String dataString = ""; // sd card script 157 158 // This sketch displays information every time a new sentence is correctly encoded. 159 while (ss.available() > 0){ 160 gps.encode(ss.read()); 161 if (gps.location.isUpdated()){ 162 Serial.print("Latitude= "); 163 Serial.print(gps.location.lat(), 6); 164 Serial.print(" Longitude= "); 165 Serial.println(gps.location.lng(), 6); 166 167 Serial.print("SSID: "); /// wifi script 168 Serial.println(WiFi.SSID()); /// wifi script 169 IPAddress ip = WiFi.localIP(); /// wifi script 170 Serial.print("IP Address: "); /// wifi script 171 Serial.println(ip); /// wifi script 172 Serial.println(dataString); // print sensor to the serial port too: 173 174 175 } 176 } 177 178 179 180 181 182 183 184 185 // Set LED colour to their Digital Ouputs. Read the input on analog pin 0: ///soil sampler code 186 int water = 9; //feeding pin to run pump for water ///soil sampler code 187 int nutrients = 10; // feeding pin for nutrients ///soil sampler code 188 int sensorValueWater = analogRead(A4); ///soil sampler code 189 int sensorValueNutrients = analogRead(A5); ///soil sampler code 190 191 int pinValue = digitalRead(8); ///Pushbotton code ///soil sampler code 192 delay(10); // quick and dirty debounce filter ///soil sampler code 193 if (buttonStatus != pinValue) { ///Pushbotton code ///soil sampler code 194 buttonStatus = pinValue; ///Pushbotton code ///soil sampler code 195 Serial.println(buttonStatus); ///Pushbotton code ///soil sampler code 196 } ///soil sampler code 197 // Set the initial state of the LEDs to OFF ///soil sampler code 198 digitalWrite(9, HIGH); //feeding pin to run pump for water Low activates relay ///soil sampler code 199 digitalWrite(10, HIGH); // feeding pin for nutrients low activated relay ///soil sampler code 200 201 202 203 204if(buttonStatus > 0) { //// in this loop we take readings and set relay to water and nutrients dispensary ///soil sampler code 205 ///soil sampler code 206 // Logic Loop that sets the required LED to ON 207 if (sensorValueWater >= 1000) (digitalWrite(water, LOW)); ////watering code 208 else if ((sensorValueWater <= 999) && (sensorValueWater >=901)) (digitalWrite(water, HIGH)); ////watering code 209 else if (sensorValueWater <= 900) (digitalWrite(water, HIGH)); ////watering code 210 else ; 211 212 213 // Prints the condition of soil. Dry, Wet or Perfect 214 if (sensorValueWater >= 1000) (Serial.print("SOIL IS TOO DRY!!!!! watering now ")); ////watering code 215 else if ((sensorValueWater<= 999) && (sensorValueWater >=901)) (Serial.print("SOIL IS PERFECT!!!!! ")); ////watering code 216 else if (sensorValueWater <= 900) (Serial.print("SOIL IS TOO WET!!!!! ")); ////watering code 217 else; 218 219 220 // print out the value you read: 221 Serial.print("Soil Humidity: "); 222 Serial.println(sensorValueWater); 223 delay(1500); // delay in between reads for stability and keep water and nutrients on for this delay 224 digitalWrite(9, HIGH); //feeding pin to run pump for water 225 226 227 228 // Logic Loop that sets the required LED to ON 229 if (sensorValueNutrients >= 1000) (digitalWrite(nutrients, LOW)); ////nutirents code 230 else if ((sensorValueNutrients <= 999) && (sensorValueNutrients >=901)) (digitalWrite(nutrients, HIGH)); ////nutrients code 231 else if (sensorValueNutrients <= 900) (digitalWrite(nutrients, HIGH)); ////nutrients code 232 else ; 233 234 235 // Prints the condition of soil. Dry, Wet or Perfect 236 if (sensorValueNutrients >= 1000) (Serial.print("nutrients are LOW dispensing now ")); ////watering code 237 else if ((sensorValueNutrients<= 999) && (sensorValueNutrients >=901)) (Serial.print("SOIL IS PERFECT!!!!! ")); ////watering code 238 else if (sensorValueNutrients <= 900) (Serial.print("SOIL IS TOO HOT IN NUTRIENTS!!! ")); ////watering code 239 else; 240 241 242 243 // print out the value you read: 244 Serial.print("Nutrients: "); 245 Serial.println(sensorValueNutrients); 246 delay(1500); // delay in between reads for stability and keep water and nutrients on for this delay 247 digitalWrite(10, HIGH); // feeding pin for nutrients 248 249 250 251 // read GPS location and append to the string: 252 for (int analogPin = 0; analogPin < 3; analogPin++) { // sd card script takes value from input 0 analog on first go thru loop 253 int sensor = analogRead(analogPin); // sd card script 254 dataString += String(sensor); // sd card script 255 if (analogPin < 2) { // sd card script 256 dataString += ",";} // sd card script 257 if (analogPin >= 2) { 258 dataString += ","; 259 dataString += "Latitude"; 260 dataString += ","; 261 dataString += String (gps.location.lat(), 6); /// dataString += (gps.location.lat(), 6); 262 dataString += ","; 263 dataString += "Longitude"; 264 dataString += ","; 265 dataString += String (gps.location.lng(), 6); 266 dataString += ","; 267 dataString += "Watersensor"; 268 dataString += ","; 269 dataString += String (sensorValueWater); 270 dataString += ","; 271 dataString += "Nutrientsensor"; 272 dataString += ","; 273 dataString += String (sensorValueNutrients); 274 dataString += ","; 275 276// open the file. note that only one file can be open at a time, 277 // so you have to close this one before opening another. 278File dataFile = SD.open("datalog.txt", FILE_WRITE); 279 280 // if the file is available, write to it: 281 if (dataFile) { 282 dataFile.println(dataString); 283 dataFile.close(); 284 } 285 // if the file isn't open, pop up an error: 286 else { 287 Serial.println("error opening datalog.txt Replace SD card reboot"); 288 } 289 } 290delay(200); 291 292 } 293 294 } 295 296 297 298 while(buttonStatus >= 1) { // this code makes sure we are only running samples once after pushing putton nat as long as button is pushed manually (gps location arrived i dnt want to run this sketch non stop) 299 toggleState=! toggleState; 300 digitalWrite(8, toggleState); 301 delay(200) ; 302 buttonStatus = digitalRead (8); 303 } 304 toggleState =! toggleState; 305 digitalWrite (8, toggleState); 306 delay(200); 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 // open the file. note that only one file can be open at a time, 349 // so you have to close this one before opening another. 350// File dataFile = SD.open("datalog.txt", FILE_WRITE); 351 352 // if the file is available, write to it: 353// if (dataFile) { 354// dataFile.println(dataString); 355// dataFile.close(); 356 // } 357 // if the file isn't open, pop up an error: 358// else { 359 // Serial.println("error opening datalog.txt Replace SD card reboot"); 360 // } 361 362 // put your main code here, to run repeatedly: 363 364 365 // LOW signal of arduino aktivates relay: 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388// driving instructions below 389if(Serial.available()){ 390} 391 392 if (Serial.available()) { 393 command = Serial.readStringUntil('\n'); 394 command.trim(); 395 if (command.equals("STOP")) { 396 digitalWrite(r1, HIGH); 397 digitalWrite(r2, HIGH); 398 digitalWrite(l1, HIGH); 399 digitalWrite(l2, HIGH); 400 delay(1000); 401 } 402 403 else if (command.equals("FORWARD")) { 404 digitalWrite(r1, LOW); 405 digitalWrite(r2, HIGH); 406 digitalWrite(l1, LOW); 407 digitalWrite(l2, HIGH); 408 delay(5000); 409 410 } 411 412 else if (command.equals("BACK")) { 413 digitalWrite(r1, HIGH); 414 digitalWrite(r2, LOW); 415 digitalWrite(l1, HIGH); 416 digitalWrite(l2, LOW); 417 delay(5000); 418 } 419 else if (command.equals("LEFT")) { 420 digitalWrite(r1, LOW); 421 digitalWrite(r2, HIGH); 422 digitalWrite(l1, HIGH); 423 digitalWrite(l2, LOW); 424 delay(2000); 425 } 426 else if (command.equals("RIGHT")) { 427 digitalWrite(r1, HIGH); 428 digitalWrite(r2, LOW); 429 digitalWrite(l1, LOW); 430 digitalWrite(l2, HIGH); 431 delay(2000); 432 } 433 434 else if (command.equals("ROW ")) { 435 digitalWrite(r1, LOW); 436 digitalWrite(r2, HIGH); 437 digitalWrite(l1, LOW); 438 digitalWrite(l2, HIGH); 439 delay(500); 440 } 441 else { 442 Serial.println("bad command"); 443 } 444 Serial.print("Command: "); 445 Serial.println(command); 446 } 447 448 449 450} 451 452 453 454 455////credits to : tutorials from 456 457///https://www.elithecomputerguy.com/author/admin/ 458///Paul McWhorter his youtube video shows step-by-step instructions
FUNCTIONING SERIAL READ OUT
arduino
WORKING CODE SERIAL READ OUT OF ip WIFI NETWORK gps LOCATION RECIVES DRIVING DRIRECTION FROM COMMAND LINE
1#include <TinyGPS++.h> 2#include <SoftwareSerial.h> 3 4 5#include <SPI.h> /// wifi script 6#include <WiFiNINA.h> /// wifi script 7#define wifiConnect 8 /// wifi script 8 9char ssid[] = "wifi network name"; /// wifi script 10char pass[] = "wifi NETWORK PASSWORD"; /// wifi script 11int keyIndex = 0; /// wifi script 12 13int status = WL_IDLE_STATUS; /// wifi script 14 15WiFiServer server(80); /// wifi script 16 17String command; ///part of beeing able to send commands from terminal line to drive 18 19 20 21 22 23static const int RXPin = 3 , TXPin = 4; 24static const uint32_t GPSBaud = 9600; 25 26 // The TinyGPS++ object of Josef McInturff 27TinyGPSPlus gps; 28 29 // The serial connection to the GPS device 30SoftwareSerial ss(RXPin, TXPin); 31 32 33 char t; // TESTING VARIBLE TO DRIVE ROBOT s stop L left R right B Backwards 34 const int l1=10,l2=11,r1=12,r2=13; /// motor driving pins connected to relay 35 36 37 38void setup(){ 39 40 pinMode(wifiConnect, OUTPUT); /// wifi script 41 42 43 //Initialize serial and wait for port to open: 44 Serial.begin(9600); 45 while (!Serial) { 46 ; // wait for serial port to connect. Needed for native USB port only 47 } 48 49 50 51 /// setup for driving the robot below 52 53 pinMode(r1,OUTPUT); // R1 right motors forward 54 pinMode(r2,OUTPUT); // R2 right motors reverse 55 pinMode(l1,OUTPUT); // L1 left motors forward 56 pinMode(l2,OUTPUT); // L2 left motors reverse 57 58 digitalWrite (r1,HIGH); //low activates relays i use set all to high 59 digitalWrite (r2,HIGH); 60 digitalWrite (l1,HIGH); 61 digitalWrite (l2,HIGH); 62 63 Serial.begin(9600); 64 ss.begin(GPSBaud); 65 66while (status != WL_CONNECTED) { /// wifi script 67 Serial.print("Attempting to connect to Network named: "); /// wifi script 68 Serial.println(ssid); /// wifi script 69 status = WiFi.begin(ssid, pass); /// wifi script 70 delay(10000); /// wifi script 71 } /// wifi script 72 73 server.begin(); /// wifi script 74 75 digitalWrite(wifiConnect, HIGH); /// wifi script 76 77 Serial.print("SSID: "); /// wifi script 78 Serial.println(WiFi.SSID()); /// wifi script 79 IPAddress ip = WiFi.localIP(); /// wifi script 80 Serial.print("IP Address: "); /// wifi script 81 Serial.println(ip); /// wifi script 82 83} 84 85void loop(){ 86 87 88 89 90 91 92 // put your main code here, to run repeatedly: 93 94 95// LOW signal of arduino aktivates relay: 96 97 98 // This sketch displays information every time a new sentence is correctly encoded. 99 while (ss.available() > 0){ 100 gps.encode(ss.read()); 101 if (gps.location.isUpdated()){ 102 Serial.print("Latitude= "); 103 Serial.print(gps.location.lat(), 6); 104 Serial.print(" Longitude= "); 105 Serial.println(gps.location.lng(), 6); 106 107 Serial.print("SSID: "); /// wifi script 108 Serial.println(WiFi.SSID()); /// wifi script 109 IPAddress ip = WiFi.localIP(); /// wifi script 110 Serial.print("IP Address: "); /// wifi script 111 Serial.println(ip); /// wifi script 112 113 114 } 115 } 116 117// driving instructions below 118 119// char t = 'F'; // TESTING VARIBLE TO DRIVE ROBOT s stop L left R right B Backwards 120 121 122 123if(Serial.available()){ 124 t = Serial.read(); 125 } 126 127// R1 right motors forward 128// R2 right motors reverse 129// L1 left motors forward 130// L2 left motors reverse 131 132 133if(t == 'F'){ //move forward(all motors rotate in forward direction) 134 digitalWrite(r1,LOW); 135 digitalWrite(r2,HIGH); 136 digitalWrite(l1,LOW); 137 digitalWrite(l2,HIGH); 138 delay(1000); 139 t = 'S'; 140 } 141 142else if(t == 'B'){ //move reverse (all motors rotate in reverse direction) 143 digitalWrite(r1,HIGH); 144 digitalWrite(r2,LOW); 145 digitalWrite(l1,HIGH); 146 digitalWrite(l2,LOW); 147 delay(1000); 148 t = 'S'; 149 } 150 151else if(t == 'L'){ //turn right (left side motors rotate in forward direction, right side motors rotate in reverse direction for 100ms & stop) 152 digitalWrite(r1,LOW); 153 digitalWrite(r2,HIGH); 154 digitalWrite(l1,HIGH); 155 digitalWrite(l2,LOW); 156 delay(1000); 157 t = 'S'; 158 } 159 160else if(t == 'R'){ //turn left (right side motors rotate in forward direction, left side motors rotate in reverse direction for 100ms & stop) 161 digitalWrite(r1,HIGH); 162 digitalWrite(r2,LOW); 163 digitalWrite(l1,LOW); 164 digitalWrite(l2,HIGH); 165 delay(1000); 166 t = 'S'; 167 } 168 169else if(t == 'S'){ //STOP (all motors stop) 170 digitalWrite(r1,HIGH); 171 digitalWrite(r2,HIGH); 172 digitalWrite(l1,HIGH); 173 digitalWrite(l2,HIGH); 174 } 175delay(100); 176 177 178 179 Serial.println("Type Command ( STOP, FORWARD, BACK, LEFT, RIGHT )"); /// sending commands via terminal 180 181delay(1000); 182 183 if (Serial.available()) { 184 command = Serial.readStringUntil('\n'); 185 command.trim(); 186 if (command.equals("STOP")) { 187 digitalWrite(r1, HIGH); 188 digitalWrite(r2, HIGH); 189 digitalWrite(l1, HIGH); 190 digitalWrite(l2, HIGH); 191delay(1000); 192 } 193 194 else if (command.equals("FORWARD")) { 195 digitalWrite(r1, LOW); 196 digitalWrite(r2, HIGH); 197 digitalWrite(l1, LOW); 198 digitalWrite(l2, HIGH); 199delay(5000); 200 201 } 202 203 else if (command.equals("BACK")) { 204 digitalWrite(r1, HIGH); 205 digitalWrite(r2, LOW); 206 digitalWrite(l1, HIGH); 207 digitalWrite(l2, LOW); 208delay(5000); 209 210 } 211 else if (command.equals("LEFT")) { 212 digitalWrite(r1, LOW); 213 digitalWrite(r2, HIGH); 214 digitalWrite(l1, HIGH); 215 digitalWrite(l2, LOW); 216delay(2000); 217} 218 else if (command.equals("RIGHT")) { 219 digitalWrite(r1, HIGH); 220 digitalWrite(r2, LOW); 221 digitalWrite(l1, LOW); 222 digitalWrite(l2, HIGH); 223delay(2000); 224} 225 226 else if (command.equals("ROW ")) { 227 digitalWrite(r1, LOW); 228 digitalWrite(r2, HIGH); 229 digitalWrite(l1, LOW); 230 digitalWrite(l2, HIGH); 231delay(500); 232 233 } 234 else { 235 Serial.println("bad command"); 236 } 237 Serial.print("Command: "); 238 Serial.println(command); 239 } 240 241 242 243} 244
Downloadable files
motor wiring
DC 12 volt reversable polarity motor schematic
motor wiring
motor wiring
DC 12 volt reversable polarity motor schematic
motor wiring
Comments
Only logged in users can leave comments
Anonymous user
2 years ago
Looks amazing! Please keep us posted! I'm about as beginner as it gets, so I can't really help sadly, but this is really inspiring. Keep up the good work M8!
mciceteade
3 years ago
Update Great Changes are coming This Project is migrating to an Arduino Mega. I simply don't have enough room on this platform. currently working on changing board out and rewriting code. while waiting for the mega to get delivered, I worked on my longest time hobby project "gasifier" go check it out if you have not yet https://www.youtube.com/watch?v=ulMT7CeU8xU
Anonymous user
3 years ago
Looks amazing! Please keep us posted! I'm about as beginner as it gets, so I can't really help sadly, but this is really inspiring. Keep up the good work M8!
mciceteade
3 years ago
This Project is ongoing
mciceteade
2 years ago
Update Great Changes are coming This Project is migrating to an Arduino Mega. I simply don't have enough room on this platform. currently working on changing board out and rewriting code. while waiting for the mega to get delivered, I worked on my longest time hobby project "gasifier" go check it out if you have not yet https://www.youtube.com/watch?v=ulMT7CeU8xU