Go Down

Topic: How can simulate until key down do writing? (Read 1 time) previous topic - next topic

isan

I write program to control motor with wiring Pi's function,in this program do digital write (for example )40 time;
How can simulate write until key down after key up stop writing?in linux
Code: [Select]
void loop() {
char ch;
scanf("%c", &ch);
if (ch == 77){
for (int steps = 0; steps < number_of_steps; steps++) {
/* 01 */
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
delay(step_delay);
/* 11 */
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, HIGH);
delay(step_delay);
/* 10 */
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
delay(step_delay);
/* 00 */
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, LOW);
delay(step_delay);
}
}

else if (ch == 75){
for (int steps = 0; steps < number_of_steps; steps++) {
/* 00 */
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, LOW);
delayMicroseconds(200);
/* 10 */
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
delayMicroseconds(200);
/* 11 */
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, HIGH);
delayMicroseconds(200);
/* 01 */
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
delayMicroseconds(200);



}
}
}

Robin2

How can simulate write until key down after key up stop writing?in linux
I don't understand what you want.

The piece of code you posted seems to make a stepper move one way when 'M' is detected and the other way when 'K' is detected.

Wouldn't it be much simpler to have
Code: [Select]
if (ch == 'M'){

Post your complete code. Not just a snippet.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

MarkT

You'll need to rephrase you question using more than one sentence - you don't explain enough about
your system to make sense of things.  Be thorough!
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

isan

I don't understand what you want.

The piece of code you posted seems to make a stepper move one way when 'M' is detected and the other way when 'K' is detected.

Wouldn't it be much simpler to have
Code: [Select]
if (ch == 'M'){

Post your complete code. Not just a snippet.

...R
yes
Code: [Select]
if (ch == 'M'){
 can use
and yes stepper move one way when 'M' is detected and the other way when 'K' is detected but for 40 time for example or in loop for ever
now I want to do write until hold 'M' after release 'M' stop writing....
my code as similar as this link    http://forum.arduino.cc/index.php?topic=143276.0

Robin2

now I want to do write until hold 'M' after release 'M' stop writing....
You can't easily do that with the Arduino Serial Monitor because what you do not want to happen is for a continuous stream of 'M's to be sent to the Arduino.

If you write a simple program on your PC using Python (or your favourite language) it can detect when a key goes down and send (say) an 'M' to start the motor moving and later detect when the key is released and send (say) and 'm' to tell the Arduino to stop the motor.

If you want to use the Serial Monitor I suggest you use 'M' to start the move and 'S' to stop the move.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

isan

#5
Nov 07, 2015, 07:56 pm Last Edit: Nov 07, 2015, 07:59 pm by isan
no I want use C++ / C for this part I should use one key hold and release
can you explain more about this part you said:

Quote
it can detect when a key goes down and send (say) an 'M' to start the motor moving and later detect when the key is released and send (say) and 'm' to tell the Arduino to stop the motor.

Robin2

can you explain more about this part you said:
I am assuming you mean that you want to  write your PC program using C/C++. I'm afraid I am too lazy for that so I can only give general guidance.

The PC system will have events that trigger when a key is pressed and when it is released. In Python the Tkinter library has an onKeyPress() function. I presume there is an equivalent for C/C++.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

isan

I think in c++ kbhit() is use and for linux i found this code:
Code: [Select]
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h>
     
    int kbhit(void)
    {
      struct termios oldt, newt;
      int ch;
      int oldf;
     
      tcgetattr(STDIN_FILENO, &oldt);
      newt = oldt;
      newt.c_lflag &= ~(ICANON | ECHO);
      tcsetattr(STDIN_FILENO, TCSANOW, &newt);
      oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
      fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
     
      ch = getchar();
     
      tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
      fcntl(STDIN_FILENO, F_SETFL, oldf);
     
      if(ch != EOF)
      {
        ungetc(ch, stdin);
        return 1;
      }
     
      return 0;
    }

Is it work for me??

isan

Other Way ,how can get how long key hold;wiringPi has function millis(); with this function can get how long any key hold??

   
Code: [Select]
key=getch();
    keyholdTime=millis();
    if(millis()<1000){

    //do some thing
    }
    else {
     // do some thing
    }

Robin2

The code in Reply #8 looks like it is Arduino code. You need PC code to deal with your problem and, as I said, I do not know enough about C/C++ on a PC to help you. I'm sure Google will have 1000s of answers.

And keyDown and keyUp do not depend on timing, they are events. Google "C++ keydown event" and see what you get

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up