Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 3ccd7fd

Browse files
Can send number of rotations
1 parent 80cc5d4 commit 3ccd7fd

File tree

2 files changed

+69
-19
lines changed

2 files changed

+69
-19
lines changed

Ceiling.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ def runTest(td):
8181

8282

8383
#======================================
84-
85-
# THE DEMO PROGRAM STARTS HERE
86-
8784
#======================================
8885

8986
import serial
@@ -106,7 +103,7 @@ def runTest(td):
106103
while 1 :
107104
print ("===========")
108105
print ("")
109-
text = input("Up 1 or Down 1?: ")
106+
text = input("Up or Down?: ")
110107
text = "<" + text + ">"
111108
runTest(text)
112109
time.sleep(1)

rodincom/rodincom.ino

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#include <Wire.h>
33
#include <Adafruit_PWMServoDriver.h>
4+
#include <String.h>
45

56
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
67

@@ -22,6 +23,7 @@ void setup() {
2223
pwm.begin();
2324
pwm.setPWMFreq(50);
2425
delay(10);
26+
pwm.setPWM(3, 0, 0);
2527
}
2628

2729
void loop() {
@@ -62,22 +64,57 @@ void recvWithStartEndMarkers() {
6264
}
6365

6466
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);
7193
}
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+
7896
}
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){
81118
Serial.println("<");
82119
Serial.print("Arduino: ");
83120
Serial.println("Moving Stopping!");
@@ -102,6 +139,22 @@ void showNewData() {
102139

103140
void readValues() {
104141
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]) : "";
106159
}
107160

0 commit comments

Comments
 (0)