Advertisement
j0h

SendKeyArduino

j0h
Jul 11th, 2023
1,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <string.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <termios.h>
  6. #include <unistd.h>
  7. //this code sends characters to the arduino and RoboKun listens..
  8. int main() {
  9.     int arduino = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
  10.     if (arduino == -1) {
  11.         perror("Is the Arduino Connected?");
  12.         return 1;
  13.     }
  14.  
  15.     struct termios options;
  16.     tcgetattr(arduino, &options);
  17.     cfsetispeed(&options, B9600);
  18.     cfsetospeed(&options, B9600);
  19.     options.c_cflag |= (CLOCAL | CREAD);
  20.     tcsetattr(arduino, TCSANOW, &options);
  21.      char w='w';
  22.      char a='a';
  23.      char s='s';
  24.      char d='d';
  25.      char b='b';
  26.      int dtime=5;
  27.     char keystrokes[] = "b";
  28.  
  29.        
  30.     keystrokes[0]=w;
  31.     printf("Forward\n");
  32.     write(arduino, keystrokes, strlen(keystrokes));
  33.     sleep(dtime);
  34.  
  35.     keystrokes[0] = d;
  36.     printf("Right\n");
  37.     write(arduino, keystrokes, strlen(keystrokes));
  38.     sleep(dtime);
  39.        
  40.      keystrokes[0] = a;
  41.     printf("LEft\n");
  42.     write(arduino, keystrokes, strlen(keystrokes));
  43.     sleep(dtime);
  44.        
  45.     keystrokes[0] = s;
  46.     printf("Reverse\n");
  47.     write(arduino, keystrokes, strlen(keystrokes));
  48.     sleep(dtime);
  49.    
  50.     keystrokes[0] = b;
  51.     printf("halt\n");
  52.     write(arduino, keystrokes, strlen(keystrokes));
  53.     sleep(1);
  54.  
  55.     close(arduino);
  56.     return 0;
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement