1

Highschool newbie here

I'm trying to create a 7 segment two-digit display (88 shaped) to display the number of times a distance sensor detects something passing in front of it

Here's what I've done so far---


#define TRIG_PIN 5
#define ECHO_PIN 6
#define light 4

void setup() {

Serial.begin(9600);

 pinMode(TRIG_PIN, OUTPUT);
 pinMode(ECHO_PIN, INPUT);
 pinMode(light, OUTPUT);
 digitalWrite(TRIG_PIN, LOW);
}


float measureDistance() {

    
    unsigned long start_time, end_time, pulse_time;


    digitalWrite(TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG_PIN, LOW);

    while (digitalRead(ECHO_PIN) == 0);

    
    start_time = micros(); 
    while (digitalRead(ECHO_PIN) == 1); 
    end_time = micros(); 
    pulse_time = end_time - start_time; 

   
    if (pulse_time > 23200) pulse_time = 23200;


    float dist_in = pulse_time / 148.0; 
    float dist_cm = pulse_time / 58.0;

    delay(60); 
    
    return dist_in;}

        int startScore = 0;

void loop() {

    float sensorDist = measureDistance();

    
    
    if (sensorDist<2){
       digitalWrite(light, HIGH); 
       int currentScore = startScore++;
       delay(500);
       Serial.println(currentScore);
    }
    else
    {
      digitalWrite(light,LOW);
    }

    }

If I'm using controllable neopixel ws2812b led strips...how do I program them to display currentScore? I'll be using two led's per segment so 14 per digit and 28 in total for the two digit display.

I am not sure how to proceed with FastLED and neopixel stuff

I found this from a youtube channel:

https://github.com/leonvandenbeukel/7-Segment-Digital-Clock-V2/blob/master/7-Segment-Clock/7-Segment-Clock.ino

This is pretty close except it's a clock

What should I do? I have little programming knowledge, sorry for the stupid questions. Sorry for my bad English. Thank you for reading :)

3
  • your english is just fine ... a lot better than many native english speakers
    – jsotola
    Mar 18 at 5:19
  • go to wokwi.com/playground/neopixel-strip ..... add this before setup() int segments[] = {255,255,0,255,0,0,0,255}; .... change this line to pixels.setPixelColor(i, segments[i], g, b); ..... move pixels.show and delay outside of for loop .... the segments array could be two dimensional ... one row for each number from 0 to 9
    – jsotola
    Mar 18 at 5:43
  • 1
    There's 2 steps to what you want to do: 1: learn how to control individual LEDs, and 2: work out what combinations of LEDs will make up what numbers. Then just light the right LED combinations depending on the values you want to display.
    – Majenko
    Mar 18 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.