1
1
2
2
#include < Wire.h>
3
3
#include < Adafruit_PWMServoDriver.h>
4
+ #include < String.h>
4
5
5
6
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
6
7
@@ -22,6 +23,7 @@ void setup() {
22
23
pwm.begin ();
23
24
pwm.setPWMFreq (50 );
24
25
delay (10 );
26
+ pwm.setPWM (3 , 0 , 0 );
25
27
}
26
28
27
29
void loop () {
@@ -62,22 +64,57 @@ void recvWithStartEndMarkers() {
62
64
}
63
65
64
66
void processData () {
65
- if (strcmp (receivedChars," Up" )==0 )
66
- {
67
- Serial.println (" <" );
68
- Serial.print (" Arduino: " );
69
- Serial.println (" Moving Up!" );
70
- pwm.setPWM (3 , 0 , 295 );
67
+ String recievedString = receivedChars;
68
+ Serial.println (" <" );
69
+ Serial.print (" RevievedString: " );
70
+ Serial.println (recievedString);
71
+ String m1_rotation_direction = getValue (recievedString, ' ,' , 0 );
72
+ String m1_rotation = getValue (recievedString, ' ,' , 1 );
73
+ int m1_rotation_int = m1_rotation.toInt ();
74
+ Serial.print (" Number of Rotations: " );
75
+ Serial.println (m1_rotation);
76
+
77
+ if (m1_rotation_direction == " Up" ) {
78
+ Serial.println (" <" );
79
+ Serial.print (" Arduino: " );
80
+ Serial.println (" Moving Up!" );
81
+ pwm.setPWM (3 , 0 , 295 );
82
+ while (1 ) {
83
+ readValues ();
84
+ Serial.println (m1_sensor_value2);
85
+ if (m1_sensor_value1 < 500 && m1_sensor_value2 > 500 ){
86
+ m1_rotation_int = m1_rotation_int - 1 ;
87
+ }
88
+
89
+ if (m1_rotation_int == 0 ){
90
+ break ;
91
+ }
92
+ delay (500 );
71
93
}
72
- else if (strcmp (receivedChars," Down" )==0 )
73
- {
74
- Serial.println (" <" );
75
- Serial.print (" Arduino: " );
76
- Serial.println (" Moving Down!" );
77
- pwm.setPWM (3 , 0 , 270 );
94
+ pwm.setPWM (3 , 0 , 0 );
95
+
78
96
}
79
- else if (strcmp (receivedChars," Stop" )==0 )
80
- {
97
+ else if (m1_rotation_direction == " Down" ) {
98
+ Serial.println (" <" );
99
+ Serial.print (" Arduino: " );
100
+ Serial.println (" Moving Down!" );
101
+ pwm.setPWM (3 , 0 , 270 );
102
+ while (1 ) {
103
+ readValues ();
104
+ Serial.println (m1_sensor_value2);
105
+ if (m1_sensor_value1 < 500 && m1_sensor_value2 > 500 ){
106
+ m1_rotation_int = m1_rotation_int - 1 ;
107
+ }
108
+
109
+ if (m1_rotation_int == 0 ){
110
+ break ;
111
+ }
112
+ delay (500 );
113
+ }
114
+ pwm.setPWM (3 , 0 , 0 );
115
+ }
116
+
117
+ else if (strcmp (receivedChars," Stop" )==0 ){
81
118
Serial.println (" <" );
82
119
Serial.print (" Arduino: " );
83
120
Serial.println (" Moving Stopping!" );
@@ -102,6 +139,22 @@ void showNewData() {
102
139
103
140
void readValues () {
104
141
m1_sensor_value1 = m1_sensor_value2;
105
- m1_sensor_value2 = analogRead (m1_sensor_pin);
142
+ m1_sensor_value2 = analogRead (m1_sensor_pin);
143
+ }
144
+
145
+ String getValue (String data, char separator, int index)
146
+ {
147
+ int found = 0 ;
148
+ int strIndex[] = { 0 , -1 };
149
+ int maxIndex = data.length () - 1 ;
150
+
151
+ for (int i = 0 ; i <= maxIndex && found <= index; i++) {
152
+ if (data.charAt (i) == separator || i == maxIndex) {
153
+ found++;
154
+ strIndex[0 ] = strIndex[1 ] + 1 ;
155
+ strIndex[1 ] = (i == maxIndex) ? i+1 : i;
156
+ }
157
+ }
158
+ return found > index ? data.substring (strIndex[0 ], strIndex[1 ]) : " " ;
106
159
}
107
160
0 commit comments