import sensor, image, time, pyb
from pyb import UART
red = pyb.LED(1)
green = pyb.LED(2)
blue = pyb.LED(3)
ir = pyb.LED(4)
uart = UART(3, 19200)
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((240, 240)) # look at center 240x240 pixels of the VGA resolution.
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
#clock = time.clock()
while(True):
#clock.tick()
img = sensor.snapshot()
#print(clock.fps())
codes = img.find_qrcodes()
for code in codes:
img.draw_rectangle(code.rect(), color = (255, 0, 0))
print(code)
if codes:
if codes[0].payload()[1] == "0":
uart.write(code[4])
if (uart.any()):
print(uart.read())
time.sleep(1000)
if codes[0].payload()[1] == "0":
uart.write(code[4])
if (uart.any()):
print(uart.read())
time.sleep(1000)
if codes[0].payload()[1] == "1":
uart.write(code[4])
if (uart.any()):
print(uart.read())
time.sleep(1000)
if codes[0].payload()[1] == "2":
uart.write(code[4])
if (uart.any()):
print(uart.read())
time.sleep(1000)
if codes[0].payload()[1] == "3":
uart.write(code[4])
if (uart.any()):
print(uart.read())
time.sleep(1000)
if codes[0].payload()[1] == "4":
uart.write(code[4])
if (uart.any()):
print(uart.read())
time.sleep(1000)
if codes[0].payload()[1] == "5":
uart.write(code[4])
if (uart.any()):
print(uart.read())
time.sleep(1000)
if codes[0].payload()[1] == "6":
uart.write(code[4])
if (uart.any()):
print(uart.read())
time.sleep(1000)
This is the openmv sketch
// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test
#include <AccelStepper.h>
#include <AFMotor.h>
#include <Servo.h>
AF_Stepper motor1(200, 1);
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
int ir1 = 8;
int ir2 = 10;
int ir3 = 11;
int irstart = 4;// connect the starting ir to these pin
//Declare pin functions on RedBoard
#define stp 2
#define dir 3
//#define MS1 4
//#define MS2 5
//#define EN 6
//Declare variables for functions
char user_input;
int x;
int y;
int state;
void StepForwardDefault()
{
Serial.println("Moving forward at default step mode.");
digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
// for (x = 0; x < 1000; x++) //Loop the forward stepping enough times for motion to be visible
for (;;)
{
digitalWrite(stp, HIGH); //Trigger one step forward
delay(1);
digitalWrite(stp, LOW); //Pull step pin low so it can be triggered again
delay(1);
}
}
void StopForwardDefault()
{
Serial.println("stopping to read default step mode.");
digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
for (x = 0; x < 1000; x++) //Loop the forward stepping enough times for motion to be visible
{
digitalWrite(stp, LOW); //Trigger one step forward
delay(1);
digitalWrite(stp, LOW); //Pull step pin low so it can be triggered again
delay(1);
}
}
AccelStepper stepper;//(forwardstep, backwardstep); // use functions to step Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
void setup() {
pinMode(stp, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(ir1, INPUT);
pinMode(ir2, INPUT);
pinMode(ir3, INPUT);
Serial.begin(19200);
myservo1.attach(6);
myservo2.attach(7); //the pin for the servo control
myservo3.attach(9);
stepper.setMaxSpeed(50);
stepper.setSpeed(50);
/*
myservo1.write(45);
// delay(30);
// myservo1.write(0);
delay(30);
myservo2.write(45);
//delay(30);
//myservo2.write(0);
delay(30);
myservo2.write(45);
//delay(30);
//myservo1.write(0);
delay(30);
*/
digitalWrite(ir1, LOW);
digitalWrite(ir2, LOW);
digitalWrite(ir3, LOW);
Serial.println("SORTING MACHINE");
Serial.println("CONFIGURINRATON DONE");
}
void loop() {
int ir1read = digitalRead(ir1);
int ir2read = digitalRead(ir2);
int ir3read = digitalRead(ir3);
////================================================
//=====================================================
if (Serial.available() > 0) {
StopForwardDefault();
mains();
}
else {
StepForwardDefault();
if (ir1read == HIGH) {
myservo1.write(45);
delay(30);
}
if (ir2read == HIGH) {
myservo3.write(45);
delay(30);
}
if (ir3read == HIGH) {
myservo3.write(45);
delay(30);
}
}
}
void mains()
{
while (Serial.available()) {
// StopForwardDefault();
delay(3); //delay to allow buffer to fill
if (Serial.available() > 0) {
char c = Serial.read(); //gets one byte from serial buffer
// Serial.println(c);
readString += c; //makes the string readString
}
}
if (readString.length() > 0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString; //.substring(0, 2); //get the first four characters
// servo2 = readString.substring(4, 8); //get the next four characters
Serial.println("CONTROL THE SERVO :: ");
Serial.print(servo1); //print to serial monitor to see parsed results
// Serial.println(servo2);
if ( servo1 .startsWith("10")) {
myservo1.write(180);
delay(30);
Serial.println("SERVO 1 MOVED ");
}
else if ( servo1 .startsWith("11")) {
myservo2.write(180);
delay(30);
Serial.println("SERVO 2 MOVED ");
}
else if ( servo1 .startsWith("12")) {
myservo3.write(180);
delay(30);
Serial.println("SERVO 3 MOVED ");
}
readString = "";
}
}
and this is the arduino code