Announcement

Collapse
No announcement yet.

Problems Reading a Button/Changing Variables

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems Reading a Button/Changing Variables

    Hi, I'm using a uOLED-96-G2 module and have multiple videos loaded onto the SD card. I'd like to have a button connected to IO2 and change videos when the button is pressed. The currently playing video is stored in the variable vidNum, and when the button is pressed, I'd like vidNum to increment and then write its current value to the SD card so it can be called up the next time the module is powered on. A pretty simple scenario.

    The problem is, almost nothing seems to be working. I have the button connected to IO2 and the module is detecting the button press, however vidNum is never getting incremented and the SD card is never being written to. I can't for the life of me find the problem.

    I have what I believe is the correct initialization sequence at the beginning:

    Code:
        pin_Set(INPUT, IO2);
        vidMax := 0;
        vidNum := 2;
        print("Starting\n") ;
        while(!media_Init())
            putstr("Drive not mounted...");
            pause(200);
            gfx_Cls();
            pause(200);
        wend
    When the program starts, here's how I'm reading the SD card to get what should have been stored as the last video played:

    Code:
        media_SetSector(0,MDA);
        setVideo := media_ReadByte();
        if(setVideo > vidMax)
            vidNum := 0;
        else
            vidNum := setVideo;
        endif
    Then here is how I'm trapping for the button press and increment of vidNum:

    Code:
                if(pin_Read(IO2)  == 0)
                    while(1 == 1)
                        // Wait until the button is released
                        putstr("Next...");
                        pause(200);
                        gfx_Cls();
                        pause(200);
                        if(pin_Read(IO2 != 0))
                            break;
                        endif
                    wend
                    vidNum := vidNum + 1;
                    if(vidNum > vidMax)
                        vidNum := 0;
                    endif
                    gfx_Cls();
    
                    // Write the new video value to the SD Card
                    media_SetSector(0,MDA);
                    media_WriteByte(vidNum);
                    media_Flush();
                endif
    I know the IO2 trapping is overly complex, but I thought the break statement might be needed to force the code to move to the vidNum increment line, because nothing else was working. The "Next..." message displays correctly when and while the button is pressed, but again, vidNum never gets incremented and nothing gets written to the SD card.

    Any idea what could be happening here?

    Thank you!

  • #2
    Hello,

    On your first code snippet, I noticed that your vidMax is initialized at zero, adding this with "if (vidNum>vidMax) vidNum := 0;" could this be the reason why vidNum is not incrementing?

    For trapping the button, I would suggest using a flag to detect if the button was previously high, something like

    Code:
    if(pin_Read(IO2)==1)
        flag :=1;
    else                 // if IO2 is low
     if (flag == 1)
        <Do stuff here>
        flag :=0
     endif
    endif
    I hope this helps.

    Best regards,
    Michael

    Comment


    • #3
      Thanks Michael, that helped. As it turns out, it was a rookie coding mistake, likely due to the fact that I was running on much too little sleep! At the beginning of the code, I had set vidMax to 0 as well, so of course vidNum was never going to increment! All fixed now, and I also implemented your flag-based approach to button reading-- works like a charm. Thanks again!

      Comment

      Working...
      X