我想将输出数据从OpenCV发送到Arduino,以控制一些执行器。我正在为苹果水果分类包装线 预先感谢
“我修改了这段代码,它可以正常工作,但是当将它连接到OpenCV时,它不能,该代码能够将数字手动发送到Arduino”
#include"stdafx.h"
using namespace System;
using namespace System::IO::Ports;
int main(array<System::String ^> ^args)
{
SerialPort port("COM7", 9600);
port.Open();
while (true)
{
int input = Convert::ToInt32(Console::ReadLine());
if (input == 1)
port.Write("R");
else if (input == 2)
port.Write("G");
else if (input == 3)
port.Write("Y");
}
return 0;
}
////// Arduino代码//// 当Arduino接收到数据(1,2,3)时,它将通过激活伺服电机来采取行动
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(motor, OUTPUT);
Servo1.attach(servoPin1);
Servo2.attach(servoPin2);
Servo1.write(0);
Servo2.write(0);
}
void loop(){
digitalWrite(motor,HIGH);
if(Serial.available())
{
int data = Serial.read();
if(data == 'R')
{
digitalWrite(led,HIGH);
Servo1.write(90);
Servo2.write(0);
}
else if (data == 'G')
{
digitalWrite(led,LOW);
Servo1.write(0);
Servo2.write(90);
}
else if (data == 'Y')
{
digitalWrite(led,HIGH);
Servo1.write(0);
Servo2.write(0);
}
//// OpenCV代码//// 当opencv检测到对象的颜色时,它将发送该颜色的number(1,2,3)指示。 1 =红色,2 =绿色,3 =黄色
if (dArea_Red > 1.15609e+07 && dArea_Red < 1.40663e+07) {
RED = 1;
std::cout << "apple type "<< RED; // Red apple
}
if (dArea_Green > 1.46945e+07 && dArea_Green < 1.6315e+07) {
GREEN = 2;
std::cout << "apple type " << GREEN; // Red apple
}
if (dArea_Red > 2.5421e+06) {
YELLOW = 3;
std::cout << "apple type " << YELLOW; // Red apple
}