• 1 Post
  • 7 Comments
Joined 1 month ago
cake
Cake day: August 18th, 2026

help-circle




  • You can try this I kinda half-arsed it though. I personally could not see the issue in your code other than the fact that you are writing bad/ugly code. I don’t mean it in a rude way, you should read other people’s code and understand the conventions. Besides that, I think the issue is with the delays.

    // This is a lot more understandable now
    #define ENTRY_FINISH_PIN 8
    #define ENTRY_NUMBER_PIN 9
    #define ENTRY_REPORT_PIN 13
    
    #define ENTRY_DELAY_MS 500
    #define ENTRY_REPORT_SHOW_MS 500
    #define ENTRY_REPORT_WAIT_MS 100
    
    int GetNumber()
    {
        // loop until ENTRY_FINISH_PIN is HIGH
        // if ENTRY_NUMBER_PIN is HIGH, add to the iterant and turn on the LED
        // after that wait a bit and turn off LED
        
        int iterant = 0;
        while (digitalRead(ENTRY_FINISH_PIN) != HIGH) {
            if (digitalRead(ENTRY_NUMBER_PIN == HIGH)) {
                iterant++;
    
                digitalWrite(ENTRY_REPORT_PIN, HIGH);
            }
    
            delay(ENTRY_DELAY_MS);
            digitalWrite(ENTRY_REPORT_PIN, LOW);
        }
        
        return iterant;
    }
    
    void setup()
    {
        pinMode(ENTRY_FINISH_PIN, INPUT);
        pinMode(ENTRY_NUMBER_PIN, INPUT);
        pinMode(ENTRY_REPORT_PIN, OUTPUT);
    }
    
    void loop()
    {
        int sum = GetNumber() + GetNumber();
    
        for (int i = 0; i < sum; i++) {
            digitalWrite(ENTRY_REPORT_PIN, HIGH);
            delay(ENTRY_REPORT_SHOW_MS);
            digitalWrite(ENTRY_REPORT_PIN, LOW);
            delay(ENTRY_REPORT_WAIT_MS); // waiting a bit more makes sense imo
        }
    }
    

    I wish u luck with your Arduino adventure ^^




  • It is not beginner friendly but it is optimized. It doesn’t allocate memory or whatever. I don’t expect you to understand all this but I did it for fun anyway.

    int print_file(const char* buffer)
    {
        // Validate the buffer
        if (!buffer || *buffer == '\0') return 1;
    
        // Prepare the first line prefix
        unsigned int line = 1;
        printf("%4d\t", line); // You could pre-format it if u want
    
        const char* cursor = buffer; // The pointer that points to the first char
        const char* linestart = cursor; // The start of the line
        char ch; // Character register
    
        while (true) {
            ch = *cursor++; // Read character THEN advance the cursor.
    
            // Check if the character is null or is newline or windows thing
    
            if (ch == '\0') {
                int linelength = cursor - linestart - 1; // Minus the null terminator
                printf("%.*s\n", linelength, linestart); // Print line using the length of string
                break;
            } else if (ch == '\n') {
                int linelength = cursor - linestart - 1; // Minus the newline
                printf("%.*s\n", linelength, linestart); // Print line using the length of string
                linestart = cursor;
    
                printf("%4d\t", ++line); // Print next line prefix
            } else if (ch == '\r') {
                continue; // Ignore the Windows thing
            }
        }
        
        return 0;
    }