11#include < SoftwareSerial.h>
22#include < Wire.h>
3- #include < Servo.h>
3+ // #include <Servo.h>
44#include < EEPROM.h>
55#include < DHT.h>
66
7+ // NOTE: Requires new libraries for screens:
8+ #include < Adafruit_GFX.h>
9+ #include < Adafruit_SSD1306.h>
10+
711void Version (){
812 Serial.println (F (" V0.4" ));
913}
1014
1115
1216SoftwareSerial *sserial = NULL ;
13- Servo servos[8 ];
17+ // Servo servos[8];
1418int servo_pins[] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1519boolean connected = false ;
1620
@@ -249,59 +253,59 @@ void pulseInSHandler(String data){
249253}
250254
251255void SV_add (String data) {
252- String sdata[3 ];
253- split (sdata,3 ,data,' %' );
254- int pin = Str2int (sdata[0 ]);
255- int min = Str2int (sdata[1 ]);
256- int max = Str2int (sdata[2 ]);
257- int pos = -1 ;
258- for (int i = 0 ; i<8 ;i++) {
259- if (servo_pins[i] == pin) { // reset in place
260- servos[pos].detach ();
261- servos[pos].attach (pin, min, max);
262- servo_pins[pos] = pin;
263- Serial.println (pos);
264- return ;
265- }
266- }
267- for (int i = 0 ; i<8 ;i++) {
268- if (servo_pins[i] == 0 ) {pos = i;break ;} // find spot in servo array
269- }
270- if (pos == -1 ) {;} // no array position available!
271- else {
272- servos[pos].attach (pin, min, max);
273- servo_pins[pos] = pin;
274- Serial.println (pos);
275- }
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+ // }
276280}
277281
278282void SV_remove (String data) {
279- int pos = Str2int (data);
280- servos[pos].detach ();
281- servo_pins[pos] = 0 ;
283+ // int pos = Str2int(data);
284+ // servos[pos].detach();
285+ // servo_pins[pos] = 0;
282286}
283287
284288void SV_read (String data) {
285- int pos = Str2int (data);
286- int angle;
287- angle = servos[pos].read ();
288- Serial.println (angle);
289+ // int pos = Str2int(data);
290+ // int angle;
291+ // angle = servos[pos].read();
292+ // Serial.println(angle);
289293}
290294
291295void SV_write (String data) {
292- String sdata[2 ];
293- split (sdata,2 ,data,' %' );
294- int pos = Str2int (sdata[0 ]);
295- int angle = Str2int (sdata[1 ]);
296- servos[pos].write (angle);
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);
297301}
298302
299303void SV_write_ms (String data) {
300- String sdata[2 ];
301- split (sdata,2 ,data,' %' );
302- int pos = Str2int (sdata[0 ]);
303- int uS = Str2int (sdata[1 ]);
304- servos[pos].writeMicroseconds (uS);
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);
305309}
306310
307311void sizeEEPROM () {
@@ -324,13 +328,13 @@ DHT dhtSensor(dhtSensorPin, DHT11);
324328void dht (String data) {
325329
326330 String sdata[2 ];
327- split (sdata, 2 , data, ' %' );
328331 int dataPin = sdata[0 ].toInt ();
329332 int sensorNumber = sdata[1 ].toInt ();
330333
331334 int sensorType = DHT11; // assume DHT11 as default
332335 if (sensorNumber == 1 ) {
333- sensorType = DHT12;
336+ // split(sdata, 2, data, '%');
337+ sensorType = DHT12;
334338 } else if (sensorNumber == 2 ) {
335339 sensorType = DHT21;
336340 } else if (sensorNumber == 2 ) {
@@ -361,6 +365,45 @@ void dht(String data) {
361365 Serial.println (String (h) + " &" + String (t) + " &" + String (hic));
362366}
363367
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 (50 );
404+ }
405+
406+ // TODO: try a switch statement, might save memory.
364407void SerialParser (void ) {
365408 char readChar[64 ];
366409 Serial.readBytesUntil (33 ,readChar,64 );
@@ -448,6 +491,10 @@ void SerialParser(void) {
448491 else if (cmd == " dht" ) {
449492 dht (data);
450493 }
494+ // screen display functions go here.
495+ else if (cmd == " dst" ) {
496+ displayText (data);
497+ }
451498}
452499
453500void setup () {
0 commit comments