Luis Pacheco

Week 14: Network and Communications

Apr 24, 2024

Intro

This weeks assignment is to communicate our microcontroller with another device throw networking, for this assingment i wanted to create an Ipad app that allows me to control the RPM, FanSpeed and temperature of my extruder control box. Also add i2c oled and SPI thermocuple amplifier to read temperature.

Objectives

  • Connect MAX31856 with SPI (wired)
  • Connect SSD1306 OLED with i2c (wired)
  • Change RPM, FanSpeed and temp with the ipad with OSC (wireless).

Tools

  • Week 8 Board
  • Arduino IDE
  • SSD1306 OLED display
  • Touch OSC
  • Adafruit MAX31856
  • K type termocouple
  • 3 Rotary Encoder Knobs

Process

I am building on top of week 8 outputs For the arduino code I had to wire connect to an Adafruit_MAX31856 with SPI and a SSD1306 OLED display with i2c Also i used some libraries to get OSC working wirelessly. For this I used the libaries:

  • Wire.h
  • Adafruit_GFX.h
  • Adafruit_SSD1306.h
  • SPI.h
  • Adafruit_MAX31856.h
  • WiFi.h
  • WiFiUdp.h
  • OSCMessage.h
  • OSCBundle.h

to connect the OLED i used the following code:

1
2
3
#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

To wire the screen was easy as its possible to use the predifined i2c from the board to communicate after this on the main loop I just run an update OLED function every few seconds to avoid this from delaying the steps generation for the motor:

Now to connect the Thermocople amplifier I has to declare each pin with SPI:

1
2
3
4
5
6
#define MAXDRDY 34
#define MAXDO   35
#define MAXCS   33
#define MAXCLK  32
#define MAXDI   13
Adafruit_MAX31856 thermocouple = Adafruit_MAX31856(MAXCS, MAXDI, MAXDO, MAXCLK);

On the main loop I check the temperature every few seconds this updates a PID function that turns the heater output on and off to try to keep the desired temperature.

Now to connect to my router and generate a static IP (for simpler communication) I used the following code:

1
2
3
4
5
6
const char* ssid = "mywifi";
const char* password = "mypassword";
// Set your Static IP address
IPAddress static_IP(192, 168, 50, 222);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

Also I added two functions one that listens if there are new OSC messages in the main loop, if there are then the second function reads the address and depending on it uses the next value and changes the desired variable for the temperature, RPM or fan speed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
void check_for_OSC_message() {
  OSCMessage msg;
  int size = udp.parsePacket();
  if (size > 0) {
    while (size--) {
      msg.fill(udp.read());
    }
    if (!msg.hasError()) {
      // Get the message address
      char msg_addr[255];
      msg.getAddress(msg_addr);
      Serial.print("MSG_ADDR: ");
      Serial.println(msg_addr);

      // if (!msg.isBundle()) {
      msg.dispatch(msg_addr, on_message_received);

    }
  }
}

void on_message_received(OSCMessage& msg) {
  // Get the LED index from the message address
  // Get the message address
  char msg_addr[255];
  msg.getAddress(msg_addr);
  Serial.println(msg_addr);
  String addr = "";
  addr += msg_addr;

  if (addr == "/MotorSpeed") {
    MotorSpeed = msg.getInt(0);
    Serial.println(MotorSpeed);
  } 

  else if (addr == "/FanSpeed") {
    FanSpeed = msg.getInt(0);
    updateFan();
    Serial.println(FanSpeed);
  }

  else if (addr == "/tarTemp") {
    tarTemp = msg.getInt(0);
    Serial.println(tarTemp);
  }

  // else if (addr == "/enable") {
  //   enable = msg.getBool(0);
  //   Serial.println(enable);
  // }
  OLED();
}

In general terms this is what the code is doing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Initialize:
  Setup Serial communication
  Setup WiFi and connect
  Initialize OLED display
  Setup temperature sensor
  Setup motor control pins
  Setup rotary encoders for motor, fan, and temperature
  Setup PWM for fan control
  Setup PID for temperature control
  Start the main loop

Main Loop:
  Check for incoming OSC messages for motor speed, fan speed, and target temperature 
    if there are new values:
        update variable
        update oled
  Check for encoder changes
    if there are changes
        Update motor 
        Update fan speed
        Update Temperature speed
        update oled
  Perform stepping operation
  Check temperature
  Get temeperature
  Run PID check
    Update heater state based on PID output
    update oled


Functions:
  Check motor encoder:
    Read encoder state
    Update motor direction based on encoder movement
  Check fan encoder:
    Read encoder state
    Update fan speed based on encoder movement
  Check temperature encoder:
    Read encoder state
    Update target temperature based on encoder movement
  Update OLED display:
    Display motor speed, fan speed, and current/target temperatures
  Handle OSC messages:
    Receive and process OSC messages for motor, fan, and temperature controls
    Update display based on changes
  Check for incoming OSC messages:
    Read UDP packets
    Dispatch messages to the appropriate handlers

This is the touchOSC app that allows to change the values on the extruder, its just three sliders,

OSC interface

In each slider define a range, address and data type:

OSC interface

now setup the UDP conection:

OSC interface

Here is a small video of the Ipad app changing the values wirelessly on the OLED screen.

And here is the arduino code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// neostruder-firmware.ino
// Luis Pacheco
//
// This work may be reproduced, modified, distributed,
// performed, and displayed for any purpose, but must
// acknowledge this project. Copyright is retained and
// must be preserved. The work is provided as is; no
// warranty is provided, and users accept all liability.
///// Import libraries///
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include "Adafruit_MAX31856.h"
#include <ezOutput.h>
#include <PID_v1.h>
#include "WiFi.h"
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>


// Display variables///
#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Rotary encoder variables
//Motor encoder
#define SPIN_A 2
#define SPIN_B 4
#define SPIN_BUTTON 15

//Fan encoder
#define FPIN_A 17
#define FPIN_B 5
#define FPIN_BUTTON 16

//temp encoder
#define TPIN_A 19
#define TPIN_B 3
#define TPIN_BUTTON 18

// Fan pwm out
#define FPIN_OUT 14
#define FPWM_Ch 1
#define FPWM_Res 8
#define FPWM_Freq 250000  //1000 or 250 000
int FPWM_DutyCycle = 0;
//#define robotIn 12  // start/stop extruder


// Global Variables for Encoder State
static int svalue = 0;
static int fvalue = 0;
static int tvalue = 0;
int DEBONCE_TO = 150;
int DEBONCE_BTN = 200;
volatile bool sturnedCW = false;
volatile bool sturnedCCW = false;
volatile bool fturnedCW = false;
volatile bool fturnedCCW = false;
volatile bool tturnedCW = false;
volatile bool tturnedCCW = false;

unsigned long slastButtonPress = 0;
unsigned long sdebounceTime = 0;

unsigned long flastButtonPress = 0;
unsigned long fdebounceTime = 0;

unsigned long tlastButtonPress = 0;
unsigned long tdebounceTime = 0;

bool slastWasCW = false;
bool slastWasCCW = false;

bool flastWasCW = false;
bool flastWasCCW = false;

bool tlastWasCW = false;
bool tlastWasCCW = false;

//// Heater Variables ////
bool enable = false;
double Output;
const unsigned long WindowSize = 5000;
unsigned long windowStartTime;
int OutHeater = 12;
int OutFan = 6;
bool HeaterEnable = false;
bool FanEnable = false;
int FanSpeed = 0;
int FanStep = 10;
double curTemp = 0;
double tarTemp = 0;
unsigned long LastReadTime = 0;

#define MAXDRDY 34
#define MAXDO   35
#define MAXCS   33
#define MAXCLK  32
#define MAXDI   13
PID myPID(&curTemp, &Output, &tarTemp, 2, 5, 1, DIRECT);
// Use software SPI: CS, DI, DO, CLK
Adafruit_MAX31856 thermocouple = Adafruit_MAX31856(MAXCS, MAXDI, MAXDO, MAXCLK);


//motor variables
int OutStep = 25;
int OutDir = 26;
int OutEnable = 27;
bool MotorEnable = false;
float MotorRev = 800;
float MotorSpeed = 0;  // rev per minute
int MotorStep = 25;
unsigned long LastRunTime = 0;
unsigned long currentMotorTime = 0;
unsigned long previousMotorTime = 0;
long motorInterval = 0.1;

bool ignoreRobotMotorSpeed = false;

// network variables:
WiFiUDP udp;
int port = 55555;


const char* ssid = "mywifi";
const char* password = "mypassword";
// Set your Static IP address
IPAddress static_IP(192, 168, 50, 222);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

void setup() {
  Serial.begin(115200);

  // Wifi Setup
  if (!WiFi.config(static_IP, gateway, subnet)) {
    Serial.println("STA Failed to configure");
  }

  // Connect to the wifi router's network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Confirm the Static Address
  Serial.print("ESP32_0 IP Address: ");
  Serial.println(WiFi.localIP());

  // Begin listening on the UDP port
  udp.begin(port);

  ///Oled setup
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {  // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
  }
  delay(2000);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  // Display static text
  OLED();

  // Setup thermocouple
   while (!Serial) delay(1); // wait for Serial on Leonardo/Zero, etc

  Serial.println("MAX31856 test");
  // wait for MAX chip to stabilize
  delay(500);
  Serial.println("Initializing sensor...");
  if (!thermocouple.begin()) {
    Serial.println("ERROR.");
    while (1) delay(10);
  }
    Serial.println("SENSOR DONE.");
    pinMode(MAXDRDY, INPUT);


  //// MOTOR setup////
  pinMode(OutDir, OUTPUT);
  pinMode(OutStep, OUTPUT);
  //outStep.low();
  pinMode(OutEnable, OUTPUT);
  digitalWrite(OutEnable, true);  // activate extruder motor
  digitalWrite(OutDir, true);     //Anti-Clockwise
  //float curSpeed = 0;
  Serial.println("Motor setup OK");
  currentMotorTime = millis();
  previousMotorTime = millis();

  //Motor Encoder setup
  pinMode(SPIN_A, INPUT_PULLUP);
  pinMode(SPIN_B, INPUT_PULLUP);
  pinMode(SPIN_BUTTON, INPUT_PULLUP);
  attachInterrupt(SPIN_B, scheckEncoder, CHANGE);
  Serial.println("Reading from encoder: MOTOR ");

  //Fan Encoder setup
  pinMode(FPIN_A, INPUT_PULLUP);
  pinMode(FPIN_B, INPUT_PULLUP);
  pinMode(FPIN_BUTTON, INPUT_PULLUP);
  attachInterrupt(FPIN_B, fcheckEncoder, CHANGE);
  Serial.println("Reading from encoder: FAN ");

  //Temp  Encoder setup
  pinMode(TPIN_A, INPUT_PULLUP);
  pinMode(TPIN_B, INPUT_PULLUP);
  pinMode(TPIN_BUTTON, INPUT_PULLUP);
  attachInterrupt(TPIN_B, tcheckEncoder, CHANGE);
  Serial.println("Reading from encoder: TEMP ");

  // Fan PWM output
  ledcSetup(FPWM_Ch, FPWM_Freq, FPWM_Res);
  ledcAttachPin(FPIN_OUT, FPWM_Ch);
  ledcWrite(FPWM_Ch, FPWM_DutyCycle);

  // Robot input
  //pinMode(robotIn, INPUT);

  //PID control
  pinMode(OutHeater, OUTPUT);
  windowStartTime = millis();

  // Tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);

  // Turn the PID on
  myPID.SetMode(AUTOMATIC);

  if (!thermocouple.begin()) {
    Serial.println("Could not initialize thermocouple.");
    while (1) delay(10);
  }
  thermocouple.setThermocoupleType(MAX31856_TCTYPE_K);
  thermocouple.setConversionMode(MAX31856_CONTINUOUS);
  // thermocouple.triggerOneShot();
  // delay(500);
  if (thermocouple.conversionComplete()) {
    curTemp = thermocouple.readThermocoupleTemperature();
    Serial.println(curTemp);
  } 
  else {
    Serial.println("Conversion not complete!");
  }
}

void loop() {
  //Check for UDP changes
  check_for_OSC_message();
  // motor control
  MotorEnable = digitalRead(true);
  // if (!digitalRead(MAXDRDY)) {
  //   curTemp = thermocouple.readThermocoupleTemperature();
  // Serial.println(curTemp);
  // }
  // if (thermocouple.conversionComplete()) {
  //   curTemp = thermocouple.readThermocoupleTemperature();
  //   Serial.println(curTemp);
  // } 
  if (MotorEnable && MotorSpeed != 0) {
    currentMotorTime = millis();
    //Serial.print(currentMotorTime);
    float StepPerMillis = MotorRev * MotorSpeed / 30000000;
    float MillisPerStep = 1 / StepPerMillis;
    //Serial.println(MillisPerStep);
    digitalWrite(OutStep, true);
    delayMicroseconds(MillisPerStep);
    digitalWrite(OutStep, false);
    delayMicroseconds(MillisPerStep);
  }
    // thermocouple.triggerOneShot();
    // if (thermocouple.conversionComplete()) {
    //   curTemp = thermocouple.readThermocoupleTemperature();
    //   Serial.println(curTemp);
    // } 
    // else {
    //   Serial.println("Conversion not complete!");
    // }
    //OLED();  // I kept your OLED update here

    // HEATER Controller with PID control only if HeaterEnable is true
    if (HeaterEnable) {
        myPID.Compute();

        unsigned long now = millis();
        if (now - windowStartTime > WindowSize) {
            // Time to shift the Relay Window
            windowStartTime += WindowSize;
        }
        if (Output > now - windowStartTime) {
            digitalWrite(OutHeater, HIGH);
            //Serial.println("heater on");
        } else {
            digitalWrite(OutHeater, LOW);
            //Serial.println("heater off");
        }
    } 
    else {
        digitalWrite(OutHeater, LOW);
        //Serial.println("heater off due to HeaterEnable being false");
    }

  // motor Rotary encoder

  if (sturnedCW) {
    svalue++;
    //MotorSpeed += MotorStep;
    MotorSpeed = svalue * MotorStep;
    //Serial.print("Turned CW: ");
    //Serial.println(svalue);
    sturnedCW = false;
    slastWasCW = true;
    sdebounceTime = millis();
    OLED();
  }

  if (sturnedCCW) {
    if (!MotorSpeed <= 0) {
      svalue--;
      //MotorSpeed -= MotorStep;
      MotorSpeed = svalue * MotorStep;
    }
    //Serial.print("Turned CCW: ");
    //Serial.println(svalue);
    sturnedCCW = false;
    slastWasCCW = true;
    sdebounceTime = millis();
    OLED();
  }

  if ((millis() - sdebounceTime) > DEBONCE_TO) {
    slastWasCW = false;
    slastWasCCW = false;
  }

  int sbtnState = (digitalRead(SPIN_BUTTON));
  // Set MotorSpeed to Zero
  if (sbtnState == LOW) {
    if (millis() - slastButtonPress > DEBONCE_BTN) {
      if (!MotorSpeed == 0) {
        // Turn off the motor speed
        MotorSpeed = 0;
        // Ignore Robot's Motor Speed

        //Serial.print('\n');
      } else {
        // Go back to previous motor speed
        MotorSpeed = svalue * MotorStep;

      }
    }
    slastButtonPress = millis();
    OLED();
  }

//Temperature rotary encoder

 if (tturnedCW) {
    tvalue++;
    tarTemp = tvalue * 5;  // Adjusting in 5-degree increments
    //Serial.print("Turned CW: ");
    //Serial.println(tvalue);
    tturnedCW = false;
    tlastWasCW = true;
    tdebounceTime = millis();
    OLED();
  }

  if (tturnedCCW) {
    if (tarTemp > 0) {   // Assuming temperature cannot go below 0.
      tvalue--;
      tarTemp = tvalue * 5;  // Adjusting in 5-degree increments
    }
    //Serial.print("Turned CCW: ");
    //Serial.println(tvalue);
    tturnedCCW = false;
    tlastWasCCW = true;
    tdebounceTime = millis();
    OLED();
  }

  if ((millis() - tdebounceTime) > DEBONCE_TO) {
    tlastWasCW = false;
    tlastWasCCW = false;
  }

  int tbtnState = (digitalRead(TPIN_BUTTON));  // Assuming you have TEMP_BUTTON for temperature control
  // Set tarTemp to Default/Initial Value
  if (tbtnState == LOW) {
    if (millis() - tlastButtonPress > DEBONCE_BTN) {
      if (tarTemp != 0) {  // Resetting the tarTemp
        tarTemp = 0;
        HeaterEnable = false;
      } else {
        // Go back to previous temperature
        tarTemp = tvalue * 5; 
        HeaterEnable = true;

      }
    }
    tlastButtonPress = millis();
    OLED();
  }

// Fan Rotary encoder

if (fturnedCW) {
  fvalue++;
  if (fvalue > 10) {
    fvalue = 10;
  }
  FanSpeed = fvalue * FanStep;
  // map(val, incoming_min, incoming_max, desired_min, desired_max);
  //Serial.println(FanSpeed);
  updateFan();
  //Serial.println(fvalue);
  fturnedCW = false;
  flastWasCW = true;
  flastWasCCW = false;
  fdebounceTime = millis();
  OLED();
}

if (fturnedCCW) {
  if (!FanSpeed == 0) {
    fvalue--;
    FanSpeed = fvalue * FanStep;
  }
  //Serial.println(FanSpeed);
  updateFan();
  //Serial.println(fvalue);
  fturnedCCW = false;
  flastWasCCW = true;
  flastWasCW = false;
  fdebounceTime = millis();
  OLED();
}

if ((millis() - fdebounceTime) > DEBONCE_TO) {
  flastWasCW = false;
  flastWasCCW = false;
}
int fbtnState = (digitalRead(FPIN_BUTTON));
if (fbtnState == LOW) {
  if (millis() - flastButtonPress > DEBONCE_BTN) {
    if (!FanSpeed == 0) {
      // Serial.println("Fan OFF");
      // Serial.println(String(FanEnable));
      FanSpeed = 0;
      FPWM_DutyCycle = map(FanSpeed, 0, 100, 0, 255);
      ledcWrite(FPWM_Ch, FPWM_DutyCycle);
      //digitalWrite(OutFan, FanEnable);
      //Serial.print('\n');
    } else {
      // Serial.println("Fan ON");
      // Serial.println(String(FanEnable));
      FanSpeed = fvalue * FanStep;
      FPWM_DutyCycle = map(FanSpeed, 0, 100, 0, 255);
      ledcWrite(FPWM_Ch, FPWM_DutyCycle);
      //digitalWrite(OutFan, FanEnable);
      //Serial.print('\n');
    }
  }
  flastButtonPress = millis();
  OLED();
}
}

void scheckEncoder() {
  if ((!sturnedCW) && (!sturnedCCW)) {
    int spinA = digitalRead(SPIN_A);
    delayMicroseconds(1500);
    int spinB = digitalRead(SPIN_B);
    if (spinA == spinB) {
      if (slastWasCW) {
        sturnedCW = true;
      } else {
        sturnedCCW = true;
      }
    } else {
      if (slastWasCCW) {
        sturnedCCW = true;
      } else {
        sturnedCW = true;
      }
    }
  }
}
void tcheckEncoder() {
  if ((!tturnedCW) && (!tturnedCCW)) {
    int tpinA = digitalRead(TPIN_A);
    delayMicroseconds(1500);
    int tpinB = digitalRead(TPIN_B);
    if (tpinA == tpinB) {
      if (tlastWasCW) {
        tturnedCW = true;
      } else {
        tturnedCCW = true;
      }
    } else {
      if (tlastWasCCW) {
        tturnedCCW = true;
      } else {
        tturnedCW = true;
      }
    }
  }
}
void fcheckEncoder() {
  if ((!fturnedCW) && (!fturnedCCW)) {
    int fpinA = digitalRead(FPIN_A);
    delayMicroseconds(1500);
    int fpinB = digitalRead(FPIN_B);
    if (fpinA == fpinB) {
      if (flastWasCW) {
        fturnedCW = true;
      } else {
        fturnedCCW = true;
      }
    } else {
      if (flastWasCCW) {
        fturnedCCW = true;
      } else {
        fturnedCW = true;
      }
    }
  }
}
void updateFan(void){
    FPWM_DutyCycle = map(FanSpeed, 0, 100, 0, 255);
    ledcWrite(FPWM_Ch, FPWM_DutyCycle);
}
void OLED(void) {
  //cTemp = module.readCelsius();
  display.clearDisplay();
  display.setCursor(0, 0);
  //display.println("Motor " + String(bool(digitalRead(robotIn))));
  display.println("Speed " + String(int(MotorSpeed)));
  display.println("Fan " + String(int(FanSpeed)) + " %");
  // display.println("H " + String(HeaterEnable));
  display.println(String(int(curTemp)) + "/" + String(int(tarTemp)) + " C");
  display.display();
}
void on_message_received(OSCMessage& msg) {
  // Get the LED index from the message address
  // Get the message address
  char msg_addr[255];
  msg.getAddress(msg_addr);
  Serial.println(msg_addr);
  String addr = "";
  addr += msg_addr;

  if (addr == "/MotorSpeed") {
    MotorSpeed = msg.getInt(0);
    Serial.println(MotorSpeed);
  } 

  else if (addr == "/FanSpeed") {
    FanSpeed = msg.getInt(0);
    updateFan();
    Serial.println(FanSpeed);
  }

  else if (addr == "/tarTemp") {
    tarTemp = msg.getInt(0);
    Serial.println(tarTemp);
  }

  // else if (addr == "/enable") {
  //   enable = msg.getBool(0);
  //   Serial.println(enable);
  // }
  OLED();
}

void check_for_OSC_message() {
  OSCMessage msg;
  int size = udp.parsePacket();
  if (size > 0) {
    while (size--) {
      msg.fill(udp.read());
    }
    if (!msg.hasError()) {
      // Get the message address
      char msg_addr[255];
      msg.getAddress(msg_addr);
      Serial.print("MSG_ADDR: ");
      Serial.println(msg_addr);

      // if (!msg.isBundle()) {
      msg.dispatch(msg_addr, on_message_received);

    }
  }
}

Conclusion

The code seems to be getting too complicated, I found an adafruit encoder board that has 4 encoders and an adafruit OLED both work with I2C also maybe the thermocuple can be connecting with i2c using less i/o and going back to the smaller XIAO or QTPY.

here is a UI test using the i2c version of the encoders and oled this UI is made with lopaka a cool tool to make interfaces:

Files

  • Touch OSC
  • Board Schematics

Copyright By Luis Pacheco