Advertisement
Faice

Hand

Nov 24th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <MMA_7455.h>
  2. #include <Wire.h>
  3.  
  4. MMA_7455 mySensor = MMA_7455(); // 가속도 센서를 불러온다
  5.  
  6. int flexpin0 = 0; // 센서값을 읽기 위해 아날로그핀 0번을 flexpin0에 지정한다
  7. int flexpin1 = 1; // 센서값을 읽기 위해 아날로그핀 1번을 flexpin1에 저장한다
  8. char xVal, yVal, zVal; // 가속도 센서의 좌표값을 저장할 변수
  9. int flexVal0, flexVal1; // 휨 센서값을 저장할 변수
  10. int flex0, flex1; // 휨 센서값을 0과 1로 변환해 임시로 저장하는 변수
  11. char arm; //R - raise, M- Middle, L - Lie 팔의 위치
  12. char drct = 'S'; //I - idle, G - go, S - stop, L - left, R - right, B - backward 차량의 방향
  13.  
  14. void setup()
  15. {
  16. Serial.begin(9600); // 센서값을 읽기 위해 시리얼 모니터를 사용할 것을 설정.
  17. mySensor.initSensitivity(2);
  18. }
  19. void loop()
  20. {
  21. xVal = mySensor.readAxis('x'); // 가속도 센서의 x좌표값을 불러들인다
  22. yVal = mySensor.readAxis('y') + 13; // y좌표값을 불러들인다
  23. zVal = mySensor.readAxis('z'); // z좌표값을 불러들인다
  24.  
  25. flexVal0 = analogRead(flexpin0); // 휨센서의 값을 아날로그로 입력 받음 (0~1023)
  26. flexVal1 = analogRead(flexpin1);
  27.  
  28. if (flexVal0 > 830){
  29. flex0 = 1;
  30. }else{
  31. flex0 = 0;
  32. }// 구부림 정도에 따라 신호값을 0 혹은 1로 한다.
  33.  
  34. if (flexVal1 > 880){
  35. flex1 = 1;
  36. }else{
  37. flex1 = 0;
  38. }
  39.  
  40. if(xVal > 40){
  41. arm = 'R';
  42. }else if(xVal < -53){
  43. arm = 'L';
  44. }else {
  45. arm = 'M';
  46. }//팔의 위치
  47.  
  48. if (flex0 == 1 && flex1 == 1){
  49. if(arm == 'R'){
  50. drct = 'S'; // Stop
  51. Serial.print(drct); //무선통신을 위한 데이터 전송
  52. }
  53. }
  54. if(flex0 == 0 && flex1 == 1){
  55. if(arm == 'M'){
  56. drct = 'G'; // Go
  57. Serial.print(drct);
  58. }
  59. }
  60. if(flex0 == 1 && flex1 == 1){
  61. if(arm == 'L'){
  62. drct = 'B'; // Backward
  63. Serial.print(drct);
  64. }
  65. }
  66. if(flex0 == 1 && flex1 == 1){
  67. // Idle
  68. if(arm == 'M'){
  69. if(yVal < -40){
  70. drct = 'L'; // Left
  71. Serial.print(drct);
  72. }else if(yVal > 45){
  73. drct = 'R'; // Right
  74. Serial.print(drct);
  75. }
  76. }
  77. }else {
  78. drct = 'I'; // Idle
  79. Serial.print(drct);
  80. }//팔의 위치와 손가락의 구부림 여부로 송신할 데이터를 판단
  81.  
  82. delay(100);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement