Bluetooth 7 segment display counter

Hi I am making a Bluetooth counter using esp 32 board. I will input a value from smartphone, and it count from the input value till 10 and then reset 0 and again start counting till 10 and so on...
but the PROBLEM I am getting is i am inputting a value from smartphone, it shows that input value for a second then it resets it to zero and no counting happens.

I've attached the code. Kindly tell me what's WRONG in the code and what all things should I've to add to run it properly....

CODE:

#include "BluetoothSerial.h"

BluetoothSerial BT;
byte val;
int pinA = 2;
int pinB = 4;
int pinC = 5;
int pinD = 18;
int pinE = 19;
int pinF = 21;
int pinG = 22;
int out1 = 23;
int a;
unsigned long prevmilli2 = 0;

long interval1 = 1000;
void setup() {
  BT.begin("esp32");
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(out1, OUTPUT);
}

void loop() {
  while (BT.available() > 0)  
  { val = BT.parseInt(); }
 
  unsigned long currmilli = millis();
  if (currmilli - prevmilli2 >= interval1) {
    val++;
    if (val > 9) { val = 0; }

    prevmilli2 = currmilli;}
  
  segmentnum(val);
  digitalWrite(out1, LOW);
}

void segmentnum(int num2dis) {
  switch (num2dis) {
    case 0:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, LOW);
      break;
    case 1:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, LOW);
      break;
    case 2:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, HIGH);
      break;
    case 3:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, HIGH);
      break;
    case 4:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      break;
    case 5:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      break;
    case 6:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      break;
    case 7:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, LOW);
      break;
    case 8:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      break;
    case 9:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      break;
  }
}

Please use Control-T to auto-format your code. It's hard to figure out what's nested where in that loop. If you format it then it will be clear.

this is the #1 debugging tool you have. You'll catch more errors just by formatting. Try it and repost the code.

1 Like

Try Serial.print and serial monitor to show what's happening in the code.

1 Like

Please edit your code to follow typical C/C++ indentation and formatting. First, split the lines that contain multiple statements. Then, auto format using ctrl-T in the IDE, and post the results in a new message.

Are you sending the value from the phone with no line endings? If you have new lines or carriage returns they will cause problems with .parseInt().

How are you sending the commands from the phone?

I've reposted the code with ehe edit plz check it

app serial bluetooth terminal

add code in your loop to print val to check what it is doing when you transmit an updated value, e.g.

void loop() {
  delay(500);
  Serial.println(val);

you will also require

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

if I run you code and send it 7 it displays

0
1
1
8
8
8
9

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.