Hi, this is my first time posting on this forum and I am still a beginner at coding Arduino. Awhile back I had code an Adafruit feather 32u4 Bluefruit le to read a the measurements from a dial indicator and send it to my phone via blue tooth. My project is based on the project by sspence here:
https://www.instructables.com/Interfacing-a-Digital-Micrometer-to-a-Microcontrol/
I now want to upgrade to the Adafruit feather ESP32 V2 to make it send the measurements directly to my computer. So first I tried to make the ESP32 V2 read the dial indicator but for some reason measurements that the V2 is showing on serial monitor is wrong. Even though I upload the exact same code that I used to test the Feather 32u4 Bluefruit le, except for changing the pin number.
byte mydata[14];
int req = 32; //mic REQ line goes to pin 5 through q1 (arduino high pulls request line low)
int dat = 13; //mic Data line goes to pin 2
int clk = 12; //mic Clock line goes to pin 3
int i = 0; int j = 0; int k = 0;
int decimal;
float dpp;
int unit;
int sign = 0;
String value_str;
long value_int;
float value;
const unsigned long timeout = 200;
void setup(){
pinMode(req, OUTPUT);
pinMode(clk, INPUT_PULLUP);
pinMode(dat, INPUT_PULLUP);
digitalWrite(req,LOW); // set request at LOW
Serial.begin(115200);
}
void loop() {
unsigned long startingtime;
startingtime = millis();
digitalWrite(req, HIGH);
for (i = 0; i < 13; i++ ) {
k = 0;
for (j = 0; j < 4; j++) {
while ((digitalRead(clk) == LOW) && ((millis() - startingtime) < timeout)){ // hold until clock is high
//Serial.println("low");
}
while ((digitalRead(clk) == HIGH) && ((millis() - startingtime) < timeout)){ // hold until clock is low
//Serial.println("high");
}
bitWrite(k, j, (digitalRead(dat) & 0x1)); // read data bits, and reverse order )
}
if (i == 0) {
digitalWrite(req, LOW);
}
mydata[i] = k;
}
sign = mydata[4];
value_str = String(mydata[5]) + String(mydata[6]) + String(mydata[7]) + String(mydata[8] + String(mydata[9] + String(mydata[10]))) ;
decimal = mydata[11];
unit = mydata[12];
value_int = value_str.toInt();
if (decimal == 0) dpp = 1.0;
if (decimal == 1) dpp = 10.0;
if (decimal == 2) dpp = 100.0;
if (decimal == 3) dpp = 1000.0;
if (decimal == 4) dpp = 10000.0;
if (decimal == 5) dpp = 100000.0;
value = value_int / dpp;
if (sign == 0) {
Serial.print(value,decimal);
}
if (sign == 8) {
Serial.print("-"); Serial.print(value,decimal);
}
if (unit == 0) {
Serial.println(" mm");
} else {
Serial.println(" in");
}
}
This is what the measurements suppose to look like in serial monitor (coming from Feather 32u4).
07:41:17.235 -> 0.001 mm
07:41:17.327 -> 0.001 mm
07:41:17.420 -> 0.001 mm
07:41:17.513 -> 0.001 mm
07:41:17.604 -> 0.001 mm
07:41:17.744 -> 0.003 mm
07:41:17.836 -> 0.004 mm
07:41:17.928 -> 0.264 mm
07:41:18.022 -> 1.213 mm
07:41:18.116 -> 2.415 mm
07:41:18.210 -> 3.167 mm
07:41:18.303 -> 3.919 mm
07:41:18.397 -> 4.648 mm
07:41:18.491 -> 5.258 mm
07:41:18.583 -> 5.864 mm
07:41:18.675 -> 6.504 mm
07:41:18.773 -> 7.002 mm
07:41:18.864 -> 7.466 mm
07:41:18.956 -> 7.945 mm
07:41:19.049 -> 8.348 mm
This is what I got using the ESP32 V2
11:08:21.203 -> in
11:08:21.386 -> in
11:08:21.480 -> 1712.011963 mm
11:08:21.537 -> in
11:08:21.761 -> in
11:08:21.854 -> 13.632000 mm
11:08:21.948 -> in
11:08:22.134 -> in
11:08:22.227 -> 2001.114014 mm
11:08:22.320 -> in
11:08:22.544 -> in
11:08:22.601 -> 7.966000 mm
11:08:22.694 -> in
11:08:22.881 -> in
11:08:22.976 -> 45.714000701904 mm
I tried to see what the raw data looks like, but sure if it can tell me or you guys anything. (raw data from feather 32u4)
08:23:07.224 -> 000000030
08:23:07.316 -> 000000030
08:23:07.410 -> 000000230
08:23:07.501 -> 000102630
08:23:07.596 -> 000229130
08:23:07.689 -> 000338630
08:23:07.785 -> 000435430
08:23:07.876 -> 000527930
08:23:07.970 -> 000603030
08:23:08.064 -> 000665430
08:23:08.156 -> 000716530
08:23:08.253 -> 000777630
08:23:08.345 -> 000827830
08:23:08.438 -> 000875430
08:23:08.530 -> 000949130
08:23:08.624 -> 001009130
08:23:08.717 -> 001081630
Raw data from ESP32 V2
08:15:09.902 -> 0026122260
08:15:09.995 -> 30412854120
08:15:10.087 -> 708835693
08:15:10.180 -> 151515151515151515
08:15:10.367 -> 10241468120
08:15:10.461 -> 70888112111
08:15:10.554 -> 151515151515151515
08:15:10.740 -> 001104430
08:15:10.832 -> 1020206120
08:15:10.925 -> 300097007
08:15:11.016 -> 151515151515151515
08:15:11.203 -> 3000413121
08:15:11.297 -> 151515151515151515
08:15:11.484 -> 1001212212120
08:15:11.578 -> 151004131414
08:15:11.671 -> 151515151515151515
08:15:11.858 -> 000254730
08:15:11.951 -> 1002812060
08:15:12.045 -> 1002805120
08:15:12.138 -> 7000121226
08:15:12.232 -> 151515151515151515
08:15:12.418 -> 000226930
08:15:12.512 -> 1008012260
08:15:12.606 -> 3004161193
08:15:12.698 -> 151515151515151515
08:15:12.884 -> 300410139120
08:15:12.977 -> 70010715613
08:15:13.070 -> 151515151515151515
08:15:13.258 -> 001271030
08:15:13.350 -> 1041280081
08:15:13.443 -> 7088111123
I don't understand why the exact same code won't work on ESP32 V2. There is no problem uploading the code, and I tried different pins but the result is still the same. I bought two ESP32 V2 broad and tried the code on both of them and the same thing happen. So I don't think it's a faulty board. Any help will be much appreciated.