isan
Posts: 3
Joined: Sat Nov 07, 2015 7:36 am

control with wire as button

Tue Nov 24, 2015 5:43 am

I have one wire that

Code: Select all

pullUpDnControl(button_pin, PUD_UP);
in raspberry gpio pin ,I want if connect to gpio pin and then connect ground pin (like button)do something if in <10000 millisecond wire connect and do something else if in >10000 millisecond wire connect,I write this code but doesn't change mode after 10000 millisecond ,how can go to start of function, get new

Code: Select all

start=millis()
and change mode every time relation with <10000 , >10000. can do it with out time(start,wait)???????

Code: Select all

void press(int pin_1, int pin_2, int key, int button_pin) {
	int reading,  reading0;
	int lastReading = HIGH;
	long start,wait;

	printf("connect button\n");
	reading = digitalRead(button_pin);
	printf("reading:%d\n", reading);

	if ((reading == HIGH) && (lastReading == HIGH)) {
		start = millis();

		while(true){
		reading0 = digitalRead(button_pin);
		printf("reading0:%d\n", reading0);
	if((reading0==LOW) && (lastReading==HIGH)){
		wait = millis();
		
		//short press
		if (wait - start<=10000) {
			//do something
			
		}

		//held key
		else if ( wait- start>10000) {
			//do some thing else
		}

	}

	}

}
}

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: control with wire as button

Tue Nov 24, 2015 11:23 am

If you add debug prints inside the 'short press'/'held key' sections:

Code: Select all

      //short press
      if (wait - start<=10000) {
         //do something
         printf("short press\n");
      }

      //held key
      else if ( wait- start>10000) {
         //do some thing else
         printf("long press\n");
      }
what is the full output you get?

If is is not in your 'do something', I assume the main problem is that the 'while(true)' loop never exits?!

Also maybe worth noting that the lastReading variable is not really needed at all.

I would clean up the code to something like:

Code: Select all

void detectPress(int button_pin) {
    int reading1, reading2;
    long start, wait;

    printf("connect button\n");
    reading1 = digitalRead(button_pin);
    printf("reading:%d\n", reading);

    if (reading1 == HIGH( {
        start = millis();
        printf("start:%d\n", start);

        bool done = false;
        while(!done){
            reading2 = digitalRead(button_pin);
            printf("reading2:%d\n", reading2);
            
            if(reading2==LOW) {
                wait = millis();

                long d = wait - start;

                //short press
                if (d <= 10000) {
                    printf("short press\n");
                    //do something
                    
                }
                //held key
                else if (d > 10000) {
                    printf("long press\n");
                    //do some thing else
                }

                done = true; // exit while
            }

        }

    }
}
Last edited by -rst- on Wed Nov 25, 2015 2:18 pm, edited 2 times in total.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

isan
Posts: 3
Joined: Sat Nov 07, 2015 7:36 am

Re: control with wire as button

Tue Nov 24, 2015 2:31 pm

tnx and what is difference between bool and Boolean ?? can use bool instead of Boolean in code??

isan
Posts: 3
Joined: Sat Nov 07, 2015 7:36 am

Re: control with wire as button

Tue Nov 24, 2015 3:20 pm

why you do this int d=wait - start , is it true cast long in int??

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: control with wire as button

Wed Nov 25, 2015 2:13 pm

isan wrote:tnx and what is difference between bool and Boolean ?? can use bool instead of Boolean in code??
To be honest I have no idea what the 'boolean' data type in your C or C++ variant is - use whatever it is meant to be ('bool' I assume, changed in the example now) - that's why I wrote 'something like' :)
isan wrote:why you do this int d=wait - start , is it true cast long in int??
Should of course be a long - my mistake, sorry (fixed there now) ...again the caveat/disclaimer 'something like' ;) Added the line just because the same (wait - start) was used twice in the code and this would look more clear and consistent (in my opinion).


Did the 'while not done' change solve your issue?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “C/C++”