Mutiple Huskylens on same arduino mega board?
![userHead](https://dfimg.dfrobot.com/dfPortrait2.png)
I'm making a robot that require multiple Huskylenses at the same time, so that i can recognize the objects coming from different angles simultaneously. But I can't find anyone who has done the same thing before. Does anyone have some ideas for how to make this work?
I've tried using:
Serial.begin(115200);
mySerial_l.begin(9600);
!huskylens_l.begin(mySerial_l);
mySerial_r.begin(9600);
!huskylens_r.begin(mySerial_r);
before, but only the later one (mySerial_r) is working.
Hello Gordan,
There are two ways I can think of that you can do to connect two Huskylenses to one board. The first method does not require a mega board: You can connect one Huskylens using soft serial ports (pin 10 and pin 11for example), and connect the other lens to the IIC port. The example code is shown below:
#include "HUSKYLENS.h"
#include "SoftwareSerial.h"
HUSKYLENS huskylens1;
HUSKYLENS huskylens;
SoftwareSerial mySerial(10, 11); // RX, TX
//SoftwareSerial mySerial1(5, 6); // RX, TX
//HUSKYLENS green line >> Pin 10; blue line >> Pin 11
void printResult(HUSKYLENSResult result);
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
// mySerial1.begin(19200);
Wire.begin();
while (!huskylens1.begin(mySerial))
{
Serial.println(F("husky1 Begin failed!"));
Serial.println(F("1.Please recheck the \"Protocol Type\" in HUSKYLENS (General Settings>>Protocol Type>>I2C)"));
Serial.println(F("2.Please recheck the connection."));
delay(100);
}
while (!huskylens.begin(Serial))
{
Serial.println(F("husky2 Begin failed!"));
Serial.println(F("1.Please recheck the \"Protocol Type\" in HUSKYLENS (General Settings>>Protocol Type>>Serial 9600)"));
Serial.println(F("2.Please recheck the connection."));
delay(100);
}
}
void loop() {
if (!huskylens.request()) Serial.println(F("Fail to request data from HUSKYLENS, recheck the connection!"));
else if(!huskylens.isLearned()) Serial.println(F("Nothing learned, press learn button on HUSKYLENS to learn one!"));
else if(!huskylens.available()) Serial.println(F("No block or arrow appears on the screen!"));
else
{
Serial.println(F("###########"));
while (huskylens.available())
{
HUSKYLENSResult result = huskylens.read();
printResult(result);
}
}
}
void printResult(HUSKYLENSResult result){
if (result.command == COMMAND_RETURN_BLOCK){
Serial.println(String()+F("Block:xCenter=")+result.xCenter+F(",yCenter=")+result.yCenter+F(",width=")+result.width+F(",height=")+result.height+F(",ID=")+result.ID);
}
else if (result.command == COMMAND_RETURN_ARROW){
Serial.println(String()+F("Arrow:xOrigin=")+result.xOrigin+F(",yOrigin=")+result.yOrigin+F(",xTarget=")+result.xTarget+F(",yTarget=")+result.yTarget+F(",ID=")+result.ID);
}
else{
Serial.println("Object unknown!");
}
}
The second way to do it is a bit tricky, you connect two Huskylenses to the hard serial port to the mega board. It requires more current as you may want to consider connecting your board to an external power source. The second code is written as follows:
#include "HUSKYLENS.h"
#include "SoftwareSerial.h"
HUSKYLENS huskylens1;
HUSKYLENS huskylens;
SoftwareSerial mySerial(10, 11); // RX, TX
//SoftwareSerial mySerial1(5, 6); // RX, TX
//HUSKYLENS green line >> Pin 10; blue line >> Pin 11
void printResult(HUSKYLENSResult result);
void setup() {
// Serial.begin(115200);
// mySerial.begin(9600);
// mySerial1.begin(19200);
Serial.begin(115200); // serial 0 PWM Pin0 Pin1
Serial1.begin(9600); // serial 1,RX Pin19 TXPin18
Serial2.begin(9600); // serial 2,RX Pin17 TX Pin16
Serial3.begin(9600); // serial 1,RX Pin15 TX Pin14
Wire.begin();
while (!huskylens1.begin(Serial1))
{
Serial.println(F("husky1 Begin failed!"));
Serial.println(F("1.Please recheck the \"Protocol Type\" in HUSKYLENS (General Settings>>Protocol Type>>I2C)"));
Serial.println(F("2.Please recheck the connection."));
delay(100);
}
while (!huskylens.begin(Serial2))
{
Serial.println(F("husky2 Begin failed!"));
Serial.println(F("1.Please recheck the \"Protocol Type\" in HUSKYLENS (General Settings>>Protocol Type>>Serial 9600)"));
Serial.println(F("2.Please recheck the connection."));
delay(100);
}
}
void loop() {
if (!huskylens.request()) Serial.println(F("Fail to request data from HUSKYLENS, recheck the connection!"));
else if (!huskylens1.request()) Serial.println(F("Fail to request data from HUSKYLENS1, recheck the connection!"));
else if(!huskylens.isLearned()) Serial.println(F("Nothing learned, press learn button on HUSKYLENS to learn one!"));
else if(!huskylens.available()) Serial.println(F("No block or arrow appears on the screen!"));
else if(!huskylens1.isLearned()) Serial.println(F("Nothing learned, press learn button on HUSKYLENS1 to learn one!"));
else if(!huskylens1.available()) Serial.println(F("No block or arrow appears on the screen111111!"));
else
{
Serial.println(F("###########"));
while (huskylens.available())
{
HUSKYLENSResult result = huskylens.read();
printResult(result);
}
Serial.println(F("###########"));
while (huskylens1.available())
{
HUSKYLENSResult result1 = huskylens1.read();
printResult(result1);
}
}
}
void printResult(HUSKYLENSResult result){
if (result.command == COMMAND_RETURN_BLOCK){
Serial.println(String()+F("Block:xCenter=")+result.xCenter+F(",yCenter=")+result.yCenter+F(",width=")+result.width+F(",height=")+result.height+F(",ID=")+result.ID);
}
else if (result.command == COMMAND_RETURN_ARROW){
Serial.println(String()+F("Arrow:xOrigin=")+result.xOrigin+F(",yOrigin=")+result.yOrigin+F(",xTarget=")+result.xTarget+F(",yTarget=")+result.yTarget+F(",ID=")+result.ID);
}
else{
Serial.println("Object unknown!");
}
}
The attached photos are how the mega board and lenses were set up using the second method:
![](https://dfimg.dfrobot.com/nobody/makelog/3a8c85eb9c767a9f4c24d9a91e43e355.jpg)
![](https://dfimg.dfrobot.com/nobody/makelog/8181c8fdfd76bac2bd6149e006183e82.jpg)
![userHeadPic](https://dfimg.dfrobot.com/dfPortrait2.png)
PS: Don’t forget to set up the protocol type(auto, IIC or Serial) on Huskylens.