Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dalewilson-eetech/09b22508b1308734bab3c92d1d92f74c to your computer and use it in GitHub Desktop.
Save dalewilson-eetech/09b22508b1308734bab3c92d1d92f74c to your computer and use it in GitHub Desktop.
All About Circuits Project - Resistor Color Code Calculator and Ohmmeter Using Arduino Uno
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define A 5
#define B 6
#define C 7
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
int startRectX = 4;
int startRectY = 39;
int startRectW = 48;
int startRectH = 15;
uint32_t colorsDigitOne[3][3] = { { 0x7a20, 0xe022, 0xeb46 }, { 0xef4f, 0x3d6b, 0x041b }, { 0x9974, 0x5aab, 0xffff } };
float valuesDigitOne[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
uint32_t colorsDigitTwo[4][3] = { { 0x0000, 0x7a20, 0xe022 }, { 0xeb46, 0xef4f, 0x3d6b }, { 0x041b, 0x9974, 0x5aab }, { 0xffff } };
float valuesDigitTwo[4][3] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9 } };
uint32_t colorsMultiplier[4][4] = { { 0x0000, 0x7a20, 0xe022 }, { 0xeb46, 0xef4f, 0x3d6b }, { 0x041b, 0x9974, 0x5aab }, { 0xffff, 0xd567, 0xbdf7 } };
float valuesMultiplier[4][4] = { { 1, 10, 100 }, { 1000, 10000, 100000 }, { 1000000, 10000000, 100000000 }, { 1000000000, 0.1, 0.01 } };
uint32_t colorsTolerance[4][3] = { { 0x7a20, 0xe022, 0xeb46 }, { 0xef4f, 0x3d6b, 0x041b }, { 0x9974, 0x5aab, 0xd567 }, { 0xbdf7 } };
float valuesTolerance[4][3] = { { 1, 2, 3 }, { 4, 0.5, .25 }, { 0.10, 0.05, 5 }, { 10 } };
unsigned long ohmMeterRanges[2][4] = { { 100, 1000, 2200, 10000 }, { 20000, 200000, 2200000, 10000000 } };
char* ohmMeterRangesChars[][4] = { { "100", "1k", "2.2k", "10k" }, { "20k", "200k", "2.2M", "10M" } };
bool muxPorts[2][4][3] = { { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 1, 0 }, { 0, 1, 1 } }, { { 1, 0, 0 }, { 1, 0, 1 }, { 1, 1, 0 }, { 1, 1, 1 } } };
unsigned long rangeValue = 0;
char rangeValueChar = ' ';
int analogPin = 0;
int data = 0;
float Vin = 5.00;
float Vout = 0;
float measuredResistor = 0;
float buffer = 0;
float valueDigitOne = 9;
float valueDigitTwo = 6;
float valueMultiplier = 0.01;
float valueTolerance = 5;
int inMenu = 0;
uint16_t colorDigitOne = colorsDigitOne[2][2]; // white
uint16_t colorDigitTwo = colorsDigitTwo[2][0]; // blue
uint16_t colorMultiplier = colorsMultiplier[3][2]; // silver
uint16_t colorTolerance = colorsTolerance[2][2]; // gold
uint16_t* colors[] = { &colorDigitOne, &colorDigitTwo, &colorMultiplier, &colorTolerance };
char* rings[] = { "Dig 1", "Dig 2", "Multi", "Tol %" };
int menuPosX = startRectX;
int menuPosY = startRectY;
int mainMenuPosX = 10;
int mainMenuPosY = 88;
int ohmMenuPosX = 10;
int ohmMenuPosY = 85;
const int buttonSelect = 4; // select
const int buttonLeft = 3; // left
const int buttonDown = 2; // down
const int buttonUp = A1; // up
const int buttonRight = A2; // right
const int buttonBack = A3; // back
float resistance = 0.96;
char abbreviation = ' ';
int flagLogo = 1;
int flagResistor = 1;
int flagRanges = 1;
int rangesY = 0;
int rangesX = 0;
void printFunction(int x, int y, uint16_t color, const char* toPrint) {
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextWrap(true);
tft.print(toPrint);
}
void printFunction(int x, int y, uint16_t color, const char toPrint) {
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextWrap(true);
tft.print(toPrint);
}
void printFunction(int x, int y, uint16_t color, const float toPrint) {
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextWrap(true);
tft.print(toPrint);
}
void menuMain() {
int buttonStateSelect = digitalRead(buttonSelect);
int buttonStateLeft = digitalRead(buttonLeft);
int buttonStateRight = digitalRead(buttonRight);
if (buttonStateRight == HIGH || buttonStateLeft == HIGH) {
delay(150);
if (mainMenuPosX < 85) {
tft.drawRoundRect(mainMenuPosX - 3, mainMenuPosY - 3, 65 + 6, 15 + 6, 4, 0xffff);
tft.drawRoundRect(mainMenuPosX - 2, mainMenuPosY - 2, 65 + 4, 15 + 4, 3, 0xffff);
mainMenuPosX += 75;
} else {
tft.drawRoundRect(mainMenuPosX - 3, mainMenuPosY - 3, 65 + 6, 15 + 6, 4, 0xffff);
tft.drawRoundRect(mainMenuPosX - 2, mainMenuPosY - 2, 65 + 4, 15 + 4, 3, 0xffff);
mainMenuPosX = 10;
}
}
tft.drawRoundRect(mainMenuPosX - 3, mainMenuPosY - 3, 65 + 6, 15 + 6, 4, 0xeb46);
tft.drawRoundRect(mainMenuPosX - 2, mainMenuPosY - 2, 65 + 4, 15 + 4, 3, 0xeb46);
if (buttonStateSelect == HIGH) {
if (mainMenuPosX == 10) {
inMenu = 1;
} else if (mainMenuPosX == 85) {
inMenu = 2;
}
delay(150);
tft.fillScreen(0xffff);
return;
}
if (flagLogo) {
printFunction(19, 92, 0x0000, "Ohmmeter");
printFunction(88, 92, 0x0000, "Calculator");
tft.drawRoundRect(10, 88, 65, 15, 3, 0x0000); // menu items
tft.drawRoundRect(85, 88, 65, 15, 3, 0x0000);
tft.fillRoundRect(53, 17, 54, 54, 7, 0xfb40); // logo rectangle
tft.fillRoundRect(57, 21, 46, 46, 4, 0xffff);
tft.fillRoundRect(46, 29, 9, 5, 1, 0xfb40); // logo outside left
tft.fillRoundRect(46, 42, 9, 5, 1, 0xfb40);
tft.fillRoundRect(46, 54, 9, 5, 2, 0xfb40);
tft.fillRoundRect(105, 29, 9, 5, 1, 0xfb40); // logo outside right
tft.fillRoundRect(105, 42, 9, 5, 1, 0xfb40);
tft.fillRoundRect(105, 54, 9, 5, 1, 0xfb40);
flagLogo = 0;
}
}
void displayResistor() {
int buttonStateSelect = digitalRead(buttonSelect);
int buttonStateBack = digitalRead(buttonBack);
int buttonStateUp = digitalRead(buttonUp);
int buttonStateDown = digitalRead(buttonDown);
if (flagResistor) { // draws the resistor using basic shapes
if (valueMultiplier < 1000) {
resistance = (valueDigitOne * 10 + valueDigitTwo) * valueMultiplier;
abbreviation = ' ';
} else if (valueMultiplier >= 1000 && valueMultiplier < 100000) {
resistance = (valueDigitOne * 10 + valueDigitTwo) * (valueMultiplier / 1000);
abbreviation = 'k';
} else if (valueMultiplier >= 100000 && valueMultiplier <= 10000000) {
resistance = (valueDigitOne * 10 + valueDigitTwo) * (valueMultiplier / 1000000);
abbreviation = 'M';
} else if (valueMultiplier > 10000000) {
resistance = (valueDigitOne * 10 + valueDigitTwo) * (valueMultiplier / 1000000000);
abbreviation = 'G';
}
tft.fillRect(4, 16, 13, 8, 0xe73c);
tft.fillRect(143, 16, 13, 8, 0xe73c);
tft.fillRect(46, 7, 68, 26, 0xD6BA);
tft.fillRoundRect(17, 4, 29, 32, 3, 0xD6BA);
tft.fillRoundRect(114, 4, 29, 32, 3, 0xD6BA);
tft.drawRect(49, 7, 8, 26, 0x0000);
tft.fillRect(50, 8, 6, 24, colorDigitOne);
tft.drawRect(70, 7, 8, 26, 0x0000);
tft.fillRect(71, 8, 6, 24, colorDigitTwo);
tft.drawRect(91, 7, 8, 26, 0x0000);
tft.fillRect(92, 8, 6, 24, colorMultiplier);
tft.drawRect(122, 4, 8, 32, 0x0000);
tft.fillRect(123, 5, 6, 30, colorTolerance);
flagResistor = 0;
}
if (buttonStateDown == HIGH) { // down
delay(150);
if (menuPosY < 102) {
tft.drawRoundRect(menuPosX + (2 * 52) - 3, menuPosY + 1, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX + (2 * 52) - 2, menuPosY + 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY += 21;
} else {
tft.drawRoundRect(menuPosX + (2 * 52) - 3, menuPosY + 1, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX + (2 * 52) - 2, menuPosY + 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY = startRectY; //48
}
}
if (buttonStateUp == HIGH) { // up
delay(150);
if (menuPosY > startRectY) {
tft.drawRoundRect(menuPosX + (2 * 52) - 3, menuPosY + 1, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX + (2 * 52) - 2, menuPosY + 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY -= 21;
} else {
tft.drawRoundRect(menuPosX + (2 * 52) - 3, menuPosY + 1, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX + (2 * 52) - 2, menuPosY + 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY = 102;
}
}
if (buttonStateSelect == HIGH) { // select
if (menuPosY == 39) {
inMenu = 3;
} else if (menuPosY == 60) {
inMenu = 4;
} else if (menuPosY == 81) {
inMenu = 5;
} else if (menuPosY == 102) {
inMenu = 6;
}
delay(150);
Serial.println(menuPosY);
menuPosX = startRectX;
menuPosY = startRectY;
tft.fillScreen(0xffff);
return;
}
if (buttonStateBack == HIGH) { // back
inMenu = 0;
tft.fillScreen(0xffff);
delay(150);
flagResistor = 1;
flagLogo = 1;
menuPosX = startRectX;
menuPosY = startRectY;
return;
}
tft.drawRoundRect(menuPosX + (2 * 52) - 3, menuPosY + 1, startRectW + 6, startRectH + 6, 4, 0xeb46);
tft.drawRoundRect(menuPosX + (2 * 52) - 2, menuPosY + 2, startRectW + 4, startRectH + 4, 3, 0xeb46);
for (int i = 0; i < 4; i++) {
printFunction(startRectX + 2 * 52 + 10, startRectY + i * 21 + 4 + 4, 0x0000, rings[i]);
tft.drawRoundRect(startRectX + 2 * 52, startRectY + i * 21 + 4, startRectW, startRectH, 3, 0x0000);
}
printFunction(6, 44, 0x0000, "Resistor");
printFunction(6, 54, 0x0000, "Color Code");
printFunction(6, 64, 0x0000, "Calculator");
printFunction(6, 83, 0x0000, "Resistance:");
printFunction(6, 93, 0x0000, resistance);
printFunction(43, 93, 0x0000, abbreviation);
printFunction(53, 93, 0x0000, "ohms");
printFunction(6, 105, 0x0000, "Tolerance:");
printFunction(6, 115, 0x0000, valueTolerance);
printFunction(37, 115, 0x0000, "%");
}
void menuDigitOne() {
int buttonStateSelect = digitalRead(buttonSelect);
int buttonStateBack = digitalRead(buttonBack);
int buttonStateLeft = digitalRead(buttonLeft);
int buttonStateRight = digitalRead(buttonRight);
int buttonStateUp = digitalRead(buttonUp);
int buttonStateDown = digitalRead(buttonDown);
printFunction(6, 15, 0x0000, "First digit:");
if (buttonStateRight == HIGH) { // right
delay(150);
if (menuPosX < 108) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX += 52;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX = startRectX;
}
}
if (buttonStateLeft == HIGH) { // left
delay(150);
if (menuPosX > startRectX) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX -= 52;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX = 108;
}
}
if (buttonStateDown == HIGH) { // down
delay(150);
if (menuPosY < 81) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY += 21;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY = startRectY;
}
}
if (buttonStateUp == HIGH) { // up
delay(150);
if (menuPosY > startRectY) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY -= 21;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY = 81;
}
}
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xeb46);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xeb46);
if (buttonStateSelect == HIGH) { // select
inMenu = 2;
valueDigitOne = valuesDigitOne[(menuPosY - startRectY) / 21][(menuPosX - startRectX) / 52];
colorDigitOne = colorsDigitOne[(menuPosY - startRectY) / 21][(menuPosX - startRectX) / 52];
Serial.println(valueDigitOne);
tft.fillScreen(0xffff);
delay(150);
flagResistor = 1;
menuPosX = startRectX;
menuPosY = startRectY;
return;
}
if (buttonStateBack == HIGH) { // back
inMenu = 2;
tft.fillScreen(0xffff);
delay(150);
flagResistor = 1;
menuPosX = startRectX;
menuPosY = startRectY;
return;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
tft.drawRoundRect(startRectX + i * 52, startRectY + j * 21, startRectW, startRectH, 3, 0x0000);
tft.fillRoundRect(startRectX + i * 52 + 1, startRectY + j * 21 + 1, startRectW - 2, startRectH - 2, 2, colorsDigitOne[j][i]);
}
}
}
void menuDigitTwo() {
int buttonStateSelect = digitalRead(buttonSelect);
int buttonStateBack = digitalRead(buttonBack);
int buttonStateLeft = digitalRead(buttonLeft);
int buttonStateRight = digitalRead(buttonRight);
int buttonStateUp = digitalRead(buttonUp);
int buttonStateDown = digitalRead(buttonDown);
printFunction(6, 15, 0x0000, "Second digit:");
if (buttonStateRight == HIGH) { // right
delay(150);
if ((menuPosX < 108) && (menuPosY < 102)) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX += 52;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX = startRectX; //48
}
}
if (buttonStateLeft == HIGH) { // left
delay(150);
if (menuPosX > startRectX) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX -= 52;
} else if (menuPosY < 102) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX = 108;
}
}
if (buttonStateDown == HIGH) { // down
delay(150);
if ((menuPosY < 81 && menuPosX != startRectX) || (menuPosY < 102 && menuPosX == startRectX)) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY += 21;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY = startRectY;
}
}
if (buttonStateUp == HIGH) { // up
delay(150);
if (menuPosY > startRectY) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY -= 21;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY = (menuPosX != startRectX) ? 81 : 102;
}
}
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xeb46);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xeb46);
if (buttonStateSelect == HIGH) { // select
inMenu = 2;
valueDigitTwo = valuesDigitTwo[(menuPosY - startRectY) / 21][(menuPosX - startRectX) / 52];
colorDigitTwo = colorsDigitTwo[(menuPosY - startRectY) / 21][(menuPosX - startRectX) / 52];
Serial.println(valueDigitTwo);
tft.fillScreen(0xffff);
delay(150);
flagResistor = 1;
menuPosX = startRectX;
menuPosY = startRectY;
return;
}
if (buttonStateBack == HIGH) { // back
inMenu = 2;
tft.fillScreen(0xffff);
delay(150);
flagResistor = 1;
menuPosX = startRectX;
menuPosY = startRectY;
return;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
tft.drawRoundRect(startRectX + i * 52, startRectY + j * 21, startRectW, startRectH, 3, 0x0000);
tft.fillRoundRect(startRectX + i * 52 + 1, startRectY + j * 21 + 1, startRectW - 2, startRectH - 2, 2, colorsDigitTwo[j][i]);
}
}
tft.drawRoundRect(startRectX + 0 * 52, startRectY + 3 * 21, startRectW, startRectH, 3, 0x0000);
tft.fillRoundRect(startRectX + 0 * 52 + 1, startRectY + 3 * 21 + 1, startRectW - 2, startRectH - 2, 2, colorsDigitTwo[3][0]);
}
void menuMultiplier() {
int buttonStateSelect = digitalRead(buttonSelect);
int buttonStateBack = digitalRead(buttonBack);
int buttonStateLeft = digitalRead(buttonLeft);
int buttonStateRight = digitalRead(buttonRight);
int buttonStateUp = digitalRead(buttonUp);
int buttonStateDown = digitalRead(buttonDown);
printFunction(6, 15, 0x0000, "Multiplier:");
if (buttonStateRight == HIGH) { // right
delay(150);
if (menuPosX < 108) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX += 52;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX = startRectX; //48
}
}
if (buttonStateLeft == HIGH) { // left
delay(150);
if (menuPosX > startRectX) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX -= 52;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX = 108;
}
}
if (buttonStateDown == HIGH) { // down
delay(150);
if (menuPosY < 102) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY += 21;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY = startRectY; //48
}
}
if (buttonStateUp == HIGH) { // up
delay(150);
if (menuPosY > startRectY) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY -= 21;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY = 102;
}
}
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xeb46);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xeb46);
if (buttonStateSelect == HIGH) { // select
inMenu = 2;
valueMultiplier = valuesMultiplier[(menuPosY - startRectY) / 21][(menuPosX - startRectX) / 52];
colorMultiplier = colorsMultiplier[(menuPosY - startRectY) / 21][(menuPosX - startRectX) / 52];
Serial.println(valueMultiplier);
tft.fillScreen(0xffff);
delay(150);
flagResistor = 1;
menuPosX = startRectX;
menuPosY = startRectY;
return;
}
if (buttonStateBack == HIGH) { // back
inMenu = 2;
tft.fillScreen(0xffff);
delay(150);
flagResistor = 1;
menuPosX = startRectX;
menuPosY = startRectY;
return;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
tft.drawRoundRect(startRectX + i * 52, startRectY + j * 21, startRectW, startRectH, 3, 0x0000);
tft.fillRoundRect(startRectX + i * 52 + 1, startRectY + j * 21 + 1, startRectW - 2, startRectH - 2, 2, colorsMultiplier[j][i]);
}
}
}
void menuTolerance() {
int buttonStateSelect = digitalRead(buttonSelect);
int buttonStateBack = digitalRead(buttonBack);
int buttonStateLeft = digitalRead(buttonLeft);
int buttonStateRight = digitalRead(buttonRight);
int buttonStateUp = digitalRead(buttonUp);
int buttonStateDown = digitalRead(buttonDown);
printFunction(6, 15, 0x0000, "Tolerance:");
if (buttonStateRight == HIGH) { // right
delay(150);
if ((menuPosX < 108) && (menuPosY < 102)) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX += 52;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX = startRectX;
}
}
if (buttonStateLeft == HIGH) { // left
delay(150);
if (menuPosX > startRectX) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX -= 52;
} else if (menuPosY < 102) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosX = 108;
}
}
if (buttonStateDown == HIGH) { // down
delay(150);
if ((menuPosY < 81 && menuPosX != startRectX) || (menuPosY < 102 && menuPosX == startRectX)) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY += 21;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY = startRectY;
}
}
if (buttonStateUp == HIGH) { // up
delay(150);
if (menuPosY > startRectY) {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY -= 21;
} else {
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xffff);
menuPosY = (menuPosX != startRectX) ? 81 : 102;
}
}
tft.drawRoundRect(menuPosX - 3, menuPosY - 3, startRectW + 6, startRectH + 6, 4, 0xeb46);
tft.drawRoundRect(menuPosX - 2, menuPosY - 2, startRectW + 4, startRectH + 4, 3, 0xeb46);
if (buttonStateSelect == HIGH) { // select
inMenu = 2;
valueTolerance = valuesTolerance[(menuPosY - startRectY) / 21][(menuPosX - startRectX) / 52];
colorTolerance = colorsTolerance[(menuPosY - startRectY) / 21][(menuPosX - startRectX) / 52];
Serial.println(valueTolerance);
tft.fillScreen(0xffff);
delay(150);
flagResistor = 1;
menuPosX = startRectX;
menuPosY = startRectY;
return;
}
if (buttonStateBack == HIGH) { // back
inMenu = 2;
tft.fillScreen(0xffff);
delay(150);
flagResistor = 1;
menuPosX = startRectX;
menuPosY = startRectY;
return;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
tft.drawRoundRect(startRectX + i * 52, startRectY + j * 21, startRectW, startRectH, 3, 0x0000);
tft.fillRoundRect(startRectX + i * 52 + 1, startRectY + j * 21 + 1, startRectW - 2, startRectH - 2, 2, colorsTolerance[j][i]);
}
}
tft.drawRoundRect(startRectX + 0 * 52, startRectY + 3 * 21, startRectW, startRectH, 3, 0x0000);
tft.fillRoundRect(startRectX + 0 * 52 + 1, startRectY + 3 * 21 + 1, startRectW - 2, startRectH - 2, 2, colorsTolerance[3][0]);
}
void menuOhmmeter() {
int buttonStateSelect = digitalRead(buttonSelect);
int buttonStateBack = digitalRead(buttonBack);
int buttonStateLeft = digitalRead(buttonLeft);
int buttonStateRight = digitalRead(buttonRight);
int buttonStateUp = digitalRead(buttonUp);
int buttonStateDown = digitalRead(buttonDown);
if (buttonStateRight == HIGH) { // right
delay(150);
if (menuPosX < 121) {
tft.drawRoundRect(menuPosX - 3, ohmMenuPosY - 3, 35 + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, ohmMenuPosY - 2, 35 + 4, startRectH + 4, 3, 0xffff);
menuPosX += 39;
} else {
tft.drawRoundRect(menuPosX - 3, ohmMenuPosY - 3, 35 + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, ohmMenuPosY - 2, 35 + 4, startRectH + 4, 3, 0xffff);
menuPosX = startRectX;
}
}
if (buttonStateLeft == HIGH) { // left
delay(150);
if (menuPosX > startRectX) {
tft.drawRoundRect(menuPosX - 3, ohmMenuPosY - 3, 35 + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, ohmMenuPosY - 2, 35 + 4, startRectH + 4, 3, 0xffff);
menuPosX -= 39;
} else {
tft.drawRoundRect(menuPosX - 3, ohmMenuPosY - 3, 35 + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, ohmMenuPosY - 2, 35 + 4, startRectH + 4, 3, 0xffff);
menuPosX = 121;
}
}
if (buttonStateDown == HIGH || buttonStateUp == HIGH) { // down or up
delay(150);
if (ohmMenuPosY == 85) {
tft.drawRoundRect(menuPosX - 3, ohmMenuPosY - 3, 35 + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, ohmMenuPosY - 2, 35 + 4, startRectH + 4, 3, 0xffff);
ohmMenuPosY += 21;
} else {
tft.drawRoundRect(menuPosX - 3, ohmMenuPosY - 3, 35 + 6, startRectH + 6, 4, 0xffff);
tft.drawRoundRect(menuPosX - 2, ohmMenuPosY - 2, 35 + 4, startRectH + 4, 3, 0xffff);
ohmMenuPosY = 85;
}
}
tft.drawRoundRect(menuPosX - 3, ohmMenuPosY - 3, 35 + 6, startRectH + 6, 4, 0xeb46);
tft.drawRoundRect(menuPosX - 2, ohmMenuPosY - 2, 35 + 4, startRectH + 4, 3, 0xeb46);
if (buttonStateSelect == HIGH) { // select
rangesY = (ohmMenuPosY - 85) / 21;
rangesX = (menuPosX - startRectX) / 39;
rangeValue = ohmMeterRanges[rangesY][rangesX];
digitalWrite(C, muxPorts[rangesY][rangesX][0]);
digitalWrite(B, muxPorts[rangesY][rangesX][1]);
digitalWrite(A, muxPorts[rangesY][rangesX][2]);
data = analogRead(analogPin);
if (data) {
buffer = data * Vin;
Vout = (buffer) / 1024.0;
buffer = (Vin / Vout) - 1;
measuredResistor = rangeValue * buffer;
Serial.print("Resistor: ");
Serial.println(measuredResistor);
delay(150);
}
Serial.println("~");
Serial.println(rangeValue);
flagRanges = 1;
delay(150);
tft.fillScreen(0xffff);
return;
}
if (buttonStateBack == HIGH) { // back
inMenu = 0;
tft.fillScreen(0xffff);
delay(150);
flagLogo = 1;
flagResistor = 1;
flagRanges = 1;
menuPosX = startRectX;
menuPosY = startRectY;
return;
}
printFunction(4, 7, 0x0000, "Ohmmeter:");
printFunction(4, 35, 0x0000, "Resistance:");
printFunction(4, 45, 0x0000, measuredResistor);
printFunction(120, 35, 0x0000, "Range:");
printFunction(120, 45, 0x0000, ohmMeterRangesChars[rangesY][rangesX]);
printFunction(4, 70, 0x0000, "Select range (ohms):");
if (flagRanges) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 2; j++) {
tft.drawRoundRect(startRectX + i * 39, 85 + j * 21, 35, startRectH, 3, 0x0000);
tft.fillRoundRect(startRectX + i * 39 + 1, 85 + j * 21 + 1, 35 - 2, startRectH - 2, 2, 0xffff);
printFunction(startRectX + i * 39 + 3, 85 + j * 21 + 3, 0x0000, ohmMeterRangesChars[j][i]);
}
}
flagRanges = 0;
}
}
void setup(void) {
Serial.begin(9600);
tft.initR(INITR_BLACKTAB); // Init ST7735S chip, green tab
tft.setRotation(3);
tft.fillScreen(0xFFFF);
pinMode(buttonLeft, INPUT);
pinMode(buttonRight, INPUT);
pinMode(buttonUp, INPUT);
pinMode(buttonDown, INPUT);
pinMode(buttonSelect, INPUT);
pinMode(buttonBack, INPUT);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
}
void loop(void) {
if (inMenu == 0) {
menuMain();
} else if (inMenu == 1) {
menuOhmmeter();
} else if (inMenu == 2) {
displayResistor();
} else if (inMenu == 3) {
menuDigitOne();
} else if (inMenu == 4) {
menuDigitTwo();
} else if (inMenu == 5) {
menuMultiplier();
} else if (inMenu == 6) {
menuTolerance();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment