Hello! Im just a beginner. Can you help me with my code? I have a Python code that sends a command/data to my Arduino code to move the 3 servo motors under a condition. My program goes like this: First, the user will input a number 1-5 then it will store it in a list. When the number falls into the same index as their number, the number/s will be popped up then the servo motors that correspond to them will move to 180 degree then will return to their original position(90 degree). For example: [5,4,3, None, None] since the number 3 is in the same index (index + 1) as its number, it will be popped up automatically like [5,4, None, None, None] then the servo motor that corresponds to it will move. Note that Servo 1 is for number 3, Servo 2 is for number 4, Servo 3 is for number 5. There are no servo motors for number 1 and 2.
Now, if there are multiple numbers that are being popped up at the same time, for example, [5, 3, 3, 5, 5], since the number 3 and number 5 are on the same index, they will pop up at the same time, [5, 3, None, 5, None]. And because they popped up at the same the, the servo motors that correspond to them (servo 1 and servo 3) should move to 180 degree all at once, they will move at the same time, then they will go back to their original position also at the same time.
I have this simple code that will rotate the servo motors at once when necessary but my problem is, I cannot implement the code that will make them return to their original position at once if there are multiple numbers that are being popped up. Can you help me with what will be the code for that? I tried to fix this but as there are multiple numbers that are being popped up, they are not moving at once, they are moving one by one.
Here's my Python code that sends an command/data to my Arduino code:
import serial
import time
ser = serial.Serial('COM3', 9600) # Replace 'COM3' with the port name of your Arduino board
def reverseFindIndex(element, array:list) -> int:
for i in range(len(array), 0, -1):
if array[i-1]==element:
return i-1
classified_array = [
None, None, None, None, # bins 1, 2, 3, 4
None, # bin 5
]
while True:
## classification
camera_output = int(input("Classification: "))
classified_array.insert(0, camera_output)
classified_array.pop()
## Before
print(f'Before: {classified_array}', end='\n\n')
## checks if marked items is equal to the target bin
for c in classified_array:
target_bin = reverseFindIndex(c, classified_array) + 1
if c == target_bin: # if the inputted numbers are on the same index
classified_array[target_bin-1] = None
print(f'Tossed at bin {target_bin}!', end='\n')
ser.write(str(target_bin).encode()) # send the bin number to Arduino
## After
print(f'After: {classified_array}', end='\n\n')
And here's my Arduino code
#include <Servo.h>
Servo servo1, servo2, servo3;
void setup() {
servo1.attach(2);
servo2.attach(3);
servo3.attach(4);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
int bin_number = Serial.read() - '0'; // convert ASCII to int
if (bin_number == 3) {
servo1.write(180);
}
if (bin_number == 4) {
servo2.write(180);
}
if (bin_number == 5) {
servo3.write(180);
}
}
}
Note: I removed the code that make them return to their original position coz it failed. I am seeking help on what should I do or add to my code to achieve my goal. I hope you help me.