Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have to build a circuit where when the sensor is sense, the green led will light up and the motor will move forward. when not sensed, the motor will reverse back, and once it completes the reverse, the red led will then turn off and the green led will then turns off.

What I have tried:

Python
import pyfirmata
import time
import math
from pyfirmata import Arduino, util

board = Arduino('COM 4')
servo = board.get_pin('d:6:s')
red = board.get_pin('d:13:o')
green = board.get_pin('d:12:o')
sensor = board.get_pin('d:7:i')


iter = pyfirmata.util.Iterator(board)
iter.start()

y = 0


positive_inf = math.inf
print('Positive Infinity: ',positive_inf)

sensor = board.get_pin('d:'+ str(sensor)+':i')
print("waiting for sensor to be Blocked")

def resumefunction():
    global y
    while True:
        if sensor.read() == 1:
            y = 1




def rotate_servo(angle):
    global y
    while True:
        if sensor.read() == 1:
            y = 1
            servo.write(angle)
            time.sleep(0.5)



while True:
    if(y == 1):
        y = 0
        green.write(0)
        red.write(1)
        board.pass_time(math.inf)





else:
    for i in range(0, 180):
        rotate_servo(i)
    for i in range(180, 0, -1):
        rotate_servo(i)
        red.write(0)
        green.write(1)
        board.pass_time(10)

while True:
    value = sensor.read()
    print("sensor has detected something")
    time.sleep(0.5)

else:
    print("sensor has not detected something")
    time.sleep(0.5)
Posted
Updated 7-Jun-21 18:56pm
v2
Comments
Richard MacCutchan 8-Jun-21 3:45am    
What happens when you run it?

1 solution

Software development has 6 distinct stages:
    1) Planning
    2) Analysis
    3) Design
    4) Coding
    5) Testing & debugging
    6) Maintenance
Any stage can reveal problems with previous work, and cause earlier stages to be revisited and changed - making it necessary to re-work all the later stages. So for example the coding stage can reveal problems in the database design, which means that the analysis stage has to be revisited, which affects the design of the software, and also means changes to existing code.

As a student, stages 1 and 2 are done by your teacher, stage 3 is generally forgotten about altogether in the rush to start coding, and you have reached the start of stage 5: testing and debugging.
This is where you prepare test data to check if your code works and to ensure that it matches the specification produced at the end of stage 2 - the homework question.

So start testing your code: provide it with inputs and decide what it should do in response: what answers it should produce. These should be "good data" and "bad data" to make certain teat it responds well to errors, as well as processes properly.
If it fails the tests, use the debugger to work out why, and revisit the coding stage to make changes in response. Then it's back to testign and debugging, until it passes all your tests and is ready for release (handing in to your teacher).

This is part of your task, (it's part of all our jobs!) so get used to it: read your question carefully, work out some test data to feed it, and try it out to see how well it complies.

That's not my job, even if I had more than your vague description of what should happen!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900