Increment value and display in C

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
chouchou
Posts: 2
Joined: Thu Dec 07, 2017 9:48 am
Contact:

Increment value and display in C

Post by chouchou »

Hi to all, want to ask some advice to me.As my primary thing with electronics is hardware, I am having some difficulties learning programming. Unfortunately, my college where I study offers no such course because my major is language, but I'm interested in programming. so for my project this year I decided to incorporate some, to force myself to learn it, however, I am struggling to understand some of it! I have been building up separate parts of my code and most of it is working, however, I am trying to display a count on an LCD010 http://www.kynix.com/Parts/3945502/LCD-010.html, and each time a button is pressed, I want it to increment by 1.

I managed to do this successfully on an Arduino (as there are many example sketches out there) but I want to do my project using an STM Nucleo (F103RB). I tried to transfer the example code over, and edit it using commands the online mbed IDE would understand, but it would not work. I then found a few different examples and each gave me different results. I eventually came up with my own code following a guide in adding/subracting in the book 'C Programming: A mordern approach'. The code I have come up with is here:

#include "mbed.h"
#include "TextLCD.h"

DigitalIn ip(D7);
TextLCD lcd(A0, A1, A2, A3, A4, A5);

int i =0;

int main() {
lcd.cls();
lcd.locate(0,0);
lcd.printf("COUNT:");
lcd.locate(0,1);
lcd.printf("%d",i);
while(1) {
if (ip ==1) {
lcd.locate(0,1);
lcd.printf("%d", ++i);
}
}
And finally, the count started responding when I pushed the button. However, the issue I am having, is that the count increments all the time I have the button pressed, so even a small press will increase the count by 50-80 (depending how quickly you press it) and I want it to increment by 1 each press.

I have tried structuring it differently, having the 'while' statement at the beginning etc, but that just caused the LCD to oscillate strangely.

I've been stuck on this for about 6 hours now before getting to this point! Would really appreciate if someone could give me some pointers in the right direction!

In case anyone is needing it, my (simplified) schematic is here:
Image
So you can see it really is very simple. But I have just zero knowledge of C programming and have never been taught how to do it properly, so any solutions will need to be explained!
Thank you!

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Increment value and display in C

Post by LeighM »

Hi,
The issue is the logic of deciding when your key is pressed and also when it is released.
So in your C, as well as detecting when the key is pressed you also need to then wait until it is released before resuming the loop.

Here is one way of doing that ...

Code: Select all

while(1) {
if (ip ==1) {
lcd.locate(0,1);
lcd.printf("%d", ++i);

while (ip == 1) {
// you could put a short delay here
// as switch bounce could be your next problem
}

}
}

chouchou
Posts: 2
Joined: Thu Dec 07, 2017 9:48 am
Contact:

Re: Increment value and display in C

Post by chouchou »

LeighM wrote:Hi,
The issue is the logic of deciding when your key is pressed and also when it is released.
So in your C, as well as detecting when the key is pressed you also need to then wait until it is released before resuming the loop.

Here is one way of doing that ...

Code: Select all

while(1) {
if (ip ==1) {
lcd.locate(0,1);
lcd.printf("%d", ++i);

while (ip == 1) {
// you could put a short delay here
// as switch bounce could be your next problem
}

}
}
Thank you so much.

Post Reply