Created
February 12, 2018 11:36
-
-
Save elktros/ec80707a22d9ab93a08cab5edfb5abba to your computer and use it in GitHub Desktop.
Python Script for Raspberry Pi based Color Detector using TCS3200 Color Sensor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time | |
s2 = 23 | |
s3 = 24 | |
signal = 25 | |
NUM_CYCLES = 10 | |
def setup(): | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(signal,GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(s2,GPIO.OUT) | |
GPIO.setup(s3,GPIO.OUT) | |
print("\n") | |
def loop(): | |
temp = 1 | |
while(1): | |
GPIO.output(s2,GPIO.LOW) | |
GPIO.output(s3,GPIO.LOW) | |
time.sleep(0.3) | |
start = time.time() | |
for impulse_count in range(NUM_CYCLES): | |
GPIO.wait_for_edge(signal, GPIO.FALLING) | |
duration = time.time() - start | |
red = NUM_CYCLES / duration | |
GPIO.output(s2,GPIO.LOW) | |
GPIO.output(s3,GPIO.HIGH) | |
time.sleep(0.3) | |
start = time.time() | |
for impulse_count in range(NUM_CYCLES): | |
GPIO.wait_for_edge(signal, GPIO.FALLING) | |
duration = time.time() - start | |
blue = NUM_CYCLES / duration | |
GPIO.output(s2,GPIO.HIGH) | |
GPIO.output(s3,GPIO.HIGH) | |
time.sleep(0.3) | |
start = time.time() | |
for impulse_count in range(NUM_CYCLES): | |
GPIO.wait_for_edge(signal, GPIO.FALLING) | |
duration = time.time() - start | |
green = NUM_CYCLES / duration | |
if green<7000 and blue<7000 and red>12000: | |
print("red") | |
temp=1 | |
elif red<12000 and blue<12000 and green>12000: | |
print("green") | |
temp=1 | |
elif green<7000 and red<7000 and blue>12000: | |
print("blue") | |
temp=1 | |
elif red>10000 and green>10000 and blue>10000 and temp==1: | |
print("place the object.....") | |
temp=0 | |
def endprogram(): | |
GPIO.cleanup() | |
if __name__=='__main__': | |
setup() | |
try: | |
loop() | |
except KeyboardInterrupt: | |
endprogram() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment