/*       CYD Analog MUX Read four sensors
           ( for carb synchronisation)
  Do what you want with this code.
  I'm sure there is a more elegant/tidy way of coding this
 
  The esp32 and display are set up as a CYD without touch.
 
 
  ## Display
  Uses the HSPI

  |Pin|Use|Note|
  |---|---|----|
  |IO2|TFT_RS|AKA: TFT_DC|
  |IO12|TFT_SDO|AKA: TFT_MISO|
  |IO13|TFT_SDI|AKA: TFT_MOSI|
  |IO14|TFT_SCK||
  |IO15|TFT_CS||
  |IO21|TFT_BL|Also on P3 connector, for some reason|
*/

//#include "User_Setup.h"
#include <TFT_eSPI.h>  // Hardware-specific library

#define S0 5
#define S1 18
const int potPin = 35;  // Analog pin

int loiter = 10;  // delay for mux to settle - might be able to reduce/remove in hardware
int potValue;       // do not change
float voltage = 0;  // do not change

TFT_eSPI tft = TFT_eSPI();  // Invoke custom library

int d, i, j, k, l, m, n = 0;
int x = 1;

int A0T = 0;
int A1T = 0;
int A2T = 0;
int A3T = 0;
int AvT = 0;

void setup(void) {

  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(potPin, INPUT);

  Serial.begin(9600);

  tft.init();
  tft.setRotation(0);  // PORTRAIT
  tft.fillScreen(TFT_BLACK);
  tft.setTextColor(TFT_WHITE);

  //********************************************
  // Draw 4 linear meters
  //********************************************
  for (i = 0; i < 240; i = i + 60) {
    tft.fillRect(2 + i, 60, 59, 230, TFT_WHITE);
    tft.drawRect(2 + i, 31, 59, 289, TFT_WHITE);
  }
  tft.setTextColor(TFT_YELLOW);
  tft.setTextSize(2);
  tft.setCursor(55, 8);
  tft.print("WMEVG MkIII");
  tft.setTextColor(TFT_WHITE);

  // Carb numbers
  for (i = 0; i < 240; i = i + 60) {
    tft.setCursor(20 + i, 38);
    tft.print("C");
    tft.print(x);  // carb number
    x = x + 1;
  }

  // draw ticks
  for (j = 60; j < 220; j = j + 60) {
    for (i = 0; i < 240; i = i + 11) {
      tft.drawLine(10 + j, 59 + i, j - 10, 59 + i, TFT_BLACK);  // ticks
    }
  }
  tft.setTextColor(TFT_YELLOW, TFT_BLACK);
}

void loop(void) {

  digitalWrite(S0, 0);  //
  digitalWrite(S1, 0);  //  sets the bits to switch the mux
  delay(loiter);
  // read the input on analog pin potPin:
  int value0 = analogRead(potPin);

  delay(loiter);
  digitalWrite(S0, 1);
  digitalWrite(S1, 0);
  delay(loiter);
  // read the input on analog pin potPin:
  int value1 = analogRead(potPin);
  delay(loiter);

  digitalWrite(S0, 0);
  digitalWrite(S1, 1);
  delay(loiter);
  // read the input on analog pin potPin:
  int value2 = analogRead(potPin);
  delay(loiter);

  digitalWrite(S0, 1);
  digitalWrite(S1, 1);
  delay(loiter);
  // read the input on analog pin potPin:
  int value3 = analogRead(potPin);
  delay(loiter);

  int valueAvg = (value0 + value1 + value2 + value3) / 4;

  Serial.print(value0);
  Serial.print(": ");
  Serial.print(value1);
  Serial.print(": ");
  Serial.print(value2);
  Serial.print(": ");
  Serial.print(value3);
  Serial.print(": ");
  Serial.println(valueAvg);



  int A0T = map(value0, 0, 4095, 289, 60);
  int A1T = map(value1, 0, 4095, 289, 60);
  int A2T = map(value2, 0, 4095, 289, 60);
  int A3T = map(value3, 0, 4095, 289, 60);
  int AvT = map(valueAvg, 0, 4095, 289, 60);

  //  }

  // horizontal pointer
  tft.drawLine(10, A0T, 50, A0T, TFT_MAGENTA);
  tft.drawLine(70, A1T, 110, A1T, TFT_MAGENTA);
  tft.drawLine(130, A2T, 170, A2T, TFT_MAGENTA);
  tft.drawLine(190, A3T, 230, A3T, TFT_MAGENTA);
  tft.drawLine(10, AvT, 230, AvT, TFT_RED);

  // value[0] = map(analogRead(A0), 0, 1023, 0, 100); // Test with value form Analogue 0

  tft.setCursor(15, 300);
  tft.print(A0T);
  Serial.print(A0T);
  Serial.print(", ");

  tft.setCursor(75, 300);
  tft.print(A1T);
  Serial.print(A1T);
  Serial.print(", ");

  tft.setCursor(135, 300);
  tft.print(A2T);
  Serial.print(A2T);
  Serial.print(", ");

  tft.setCursor(195, 300);
  tft.print(A3T);
  Serial.println(A3T);
  delay(500);

  tft.drawLine(10, A0T, 50, A0T, TFT_WHITE);
  tft.drawLine(70, A1T, 110, A1T, TFT_WHITE);
  tft.drawLine(130, A2T, 170, A2T, TFT_WHITE);
  tft.drawLine(190, A3T, 230, A3T, TFT_WHITE);
  tft.drawLine(10, AvT, 230, AvT, TFT_WHITE);

  // redraw ticks
  for (j = 60; j < 220; j = j + 60) {
    for (i = 0; i < 240; i = i + 11) {
      tft.drawLine(10 + j, 59 + i, j - 10, 59 + i, TFT_BLACK);  // ticks
    }
  }
  // redraw verts
  for (k = 61; k < 240; k = k + 60) {
    tft.drawLine(k, 60, k, 320, TFT_BLACK);  // verts
  }
}
Loading
cd74hc4067