11#include < SoftwareSerial.h>
22#include < Wire.h>
3- // #include <Servo.h>
3+ #include < Servo.h>
44#include < EEPROM.h>
5- #include < DHT.h>
6-
7- // NOTE: Requires new libraries for screens:
8- #include < Adafruit_GFX.h>
9- #include < Adafruit_SSD1306.h>
105
116void Version (){
12- Serial.println (F (" V0.5 " ));
7+ Serial.println (F (" V0.6 " ));
138}
149
1510
1611SoftwareSerial *sserial = NULL ;
17- // Servo servos[8];
12+ Servo servos[8 ];
1813int servo_pins[] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1914boolean connected = false ;
2015
@@ -253,59 +248,59 @@ void pulseInSHandler(String data){
253248}
254249
255250void SV_add (String data) {
256- // String sdata[3];
257- // split(sdata,3,data,'%');
258- // int pin = Str2int(sdata[0]);
259- // int min = Str2int(sdata[1]);
260- // int max = Str2int(sdata[2]);
261- // int pos = -1;
262- // for (int i = 0; i<8;i++) {
263- // if (servo_pins[i] == pin) { //reset in place
264- // servos[pos].detach();
265- // servos[pos].attach(pin, min, max);
266- // servo_pins[pos] = pin;
267- // Serial.println(pos);
268- // return;
269- // }
270- // }
271- // for (int i = 0; i<8;i++) {
272- // if (servo_pins[i] == 0) {pos = i;break;} // find spot in servo array
273- // }
274- // if (pos == -1) {;} //no array position available!
275- // else {
276- // servos[pos].attach(pin, min, max);
277- // servo_pins[pos] = pin;
278- // Serial.println(pos);
279- // }
251+ String sdata[3 ];
252+ split (sdata,3 ,data,' %' );
253+ int pin = Str2int (sdata[0 ]);
254+ int min = Str2int (sdata[1 ]);
255+ int max = Str2int (sdata[2 ]);
256+ int pos = -1 ;
257+ for (int i = 0 ; i<8 ;i++) {
258+ if (servo_pins[i] == pin) { // reset in place
259+ servos[pos].detach ();
260+ servos[pos].attach (pin, min, max);
261+ servo_pins[pos] = pin;
262+ Serial.println (pos);
263+ return ;
264+ }
265+ }
266+ for (int i = 0 ; i<8 ;i++) {
267+ if (servo_pins[i] == 0 ) {pos = i;break ;} // find spot in servo array
268+ }
269+ if (pos == -1 ) {;} // no array position available!
270+ else {
271+ servos[pos].attach (pin, min, max);
272+ servo_pins[pos] = pin;
273+ Serial.println (pos);
274+ }
280275}
281276
282277void SV_remove (String data) {
283- // int pos = Str2int(data);
284- // servos[pos].detach();
285- // servo_pins[pos] = 0;
278+ int pos = Str2int (data);
279+ servos[pos].detach ();
280+ servo_pins[pos] = 0 ;
286281}
287282
288283void SV_read (String data) {
289- // int pos = Str2int(data);
290- // int angle;
291- // angle = servos[pos].read();
292- // Serial.println(angle);
284+ int pos = Str2int (data);
285+ int angle;
286+ angle = servos[pos].read ();
287+ Serial.println (angle);
293288}
294289
295290void SV_write (String data) {
296- // String sdata[2];
297- // split(sdata,2,data,'%');
298- // int pos = Str2int(sdata[0]);
299- // int angle = Str2int(sdata[1]);
300- // servos[pos].write(angle);
291+ String sdata[2 ];
292+ split (sdata,2 ,data,' %' );
293+ int pos = Str2int (sdata[0 ]);
294+ int angle = Str2int (sdata[1 ]);
295+ servos[pos].write (angle);
301296}
302297
303298void SV_write_ms (String data) {
304- // String sdata[2];
305- // split(sdata,2,data,'%');
306- // int pos = Str2int(sdata[0]);
307- // int uS = Str2int(sdata[1]);
308- // servos[pos].writeMicroseconds(uS);
299+ String sdata[2 ];
300+ split (sdata,2 ,data,' %' );
301+ int pos = Str2int (sdata[0 ]);
302+ int uS = Str2int (sdata[1 ]);
303+ servos[pos].writeMicroseconds (uS);
309304}
310305
311306void sizeEEPROM () {
@@ -322,88 +317,6 @@ void EEPROMHandler(int mode, String data) {
322317 }
323318}
324319
325- int dhtSensorPin = -1 ;
326- DHT dhtSensor (dhtSensorPin, DHT11);
327-
328- void dht (String data) {
329-
330- String sdata[2 ];
331- split (sdata, 2 , data, ' %' );
332- int dataPin = sdata[0 ].toInt ();
333- int sensorNumber = sdata[1 ].toInt ();
334-
335- int sensorType = DHT11; // assume DHT11 as default
336- if (sensorNumber == 1 ) {
337- sensorType = DHT12;
338- } else if (sensorNumber == 2 ) {
339- sensorType = DHT21;
340- } else if (sensorNumber == 2 ) {
341- sensorType = DHT22;
342- } else if (sensorNumber == 2 ) {
343- sensorType = AM2301;
344- }
345-
346- // do not initialize new sensor if we are reading repeatedly from same sensor
347- if (dataPin != dhtSensorPin) {
348- dhtSensorPin = dataPin;
349- dhtSensor = DHT (dataPin, sensorType);
350- dhtSensor.begin ();
351- }
352-
353- // Reading temperature or humidity takes about 250 milliseconds!
354- // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
355- float h = dhtSensor.readHumidity ();
356- // Read temperature as Celsius (the default)
357- float t = dhtSensor.readTemperature ();
358-
359- if (isnan (h) || isnan (t)) {
360- Serial.println (" 0&0&0" );
361- return ;
362- }
363-
364- float hic = dhtSensor.computeHeatIndex (t, h, false );
365- Serial.println (String (h) + " &" + String (t) + " &" + String (hic));
366- }
367-
368-
369- // TODO: Fix the stuttering problem being caused by the program calling the display parameter multiple times.
370-
371- // A large function to set up the display, clear it from previously, set a line(s) of text, and write it.
372- // TODO: I was unable to break this apart into different functions to play around with in Python, due to issues with variable scope. I will come back to this.
373- void displayText (String data) {
374-
375- int screen_height = 32 ;
376- int screen_width = 128 ;
377-
378- // The analog pin number connected to the reset pin of the screen (SDA).
379- int reset_pin = 4 ;
380-
381- // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins).
382- Adafruit_SSD1306 display (screen_width, screen_height, &Wire, 4 );
383-
384- display.begin (SSD1306_SWITCHCAPVCC, 0x3C );
385-
386- // Clears previously displayed data and resets the cursor and text colour.
387- display.clearDisplay ();
388- display.setCursor (0 ,0 );
389- display.setTextColor (WHITE);
390-
391- // The input data string contains the text to be written, along with %#,
392- // where # is the font size. This sets the font size by reading the last
393- // character of the input data (by default it is 1), and converting it
394- // to an int. Once that is done, the last two characters are deleted.
395- int font_size = data[data.length () - 1 ] - 48 ;
396- display.setTextSize (font_size);
397- data.remove (data.length () - 2 );
398-
399- display.print (data);
400-
401- // Prints the above to the display. Relatively resource-intensive.
402- display.display ();
403- delay (100 );
404- }
405-
406- // TODO: try a switch statement, might save memory.
407320void SerialParser (void ) {
408321 char readChar[64 ];
409322 Serial.readBytesUntil (33 ,readChar,64 );
@@ -488,13 +401,6 @@ void SerialParser(void) {
488401 else if (cmd == " sz" ) {
489402 sizeEEPROM ();
490403 }
491- else if (cmd == " dht" ) {
492- dht (data);
493- }
494- // screen display functions go here.
495- else if (cmd == " dst" ) {
496- displayText (data);
497- }
498404}
499405
500406void setup () {
0 commit comments