Displaying data from multiple IR sensors in one screen*

Hey i have a screen wich contains 6 different labels (each label represents a single IR sensor , so six sensors in total ), those labels's background colors can be either red or green , depending on the value of the sensor , for example : if sensor number 1 detects something , th background color of the first label will become red , and when it stops detecting , it becomes green . it worked with one sensor , but i want to do that with all 6 of my sensors at the same time , and idk how so it would be great if u guys can help me . this is a school project and and i need to finish it ASAP so plz provide me with ur help guys

You didn't mention how the IR sensors communicate with your phone.

1 Like

i reedited the reply bc i shared the wrong screenshots sry
I use the HC-05 bluetooth module to communicate between the phone and the sensor.
i tried 2 different things :
1st try :

Here is the code for this :

#include <SoftwareSerial.h>
const int IR1 = 4;
const int IR2 = 5;
const int IR3 = 6;
const int IR4 = 7;
const int IR5 = 8;
const int IR6 = 9;
int value1;
int value2;
int value3;
int value4;
int value5;
int value6;
void setup() {
Serial.begin(9600);
pinMode(IR1,INPUT);
pinMode(IR2,INPUT);
pinMode(IR3,INPUT);
pinMode(IR4,INPUT);
pinMode(IR5,INPUT);
pinMode(IR6,INPUT);

}
void loop() {
value1 = digitalRead(IR1);
value2= digitalRead(IR2);
value3= digitalRead(IR3);
value4= digitalRead(IR4);
value5= digitalRead(IR5);
value6= digitalRead(IR6);
delay(1000);
if (value1==LOW){

Serial.print("d");delay(1000);
}
else {
Serial.print("n");delay(1000);
}

if (value2==LOW){

Serial.print("e");delay(1000);
}
else {
Serial.print("a");delay(1000);
}
if (value3==LOW){

Serial.print("f");delay(1000);
}
else {
Serial.print("b");delay(1000);
}
if (value4==LOW){

Serial.print("g");delay(1000);
}
else {
Serial.print("b");delay(1000);
}
if (value5==LOW){

Serial.print("h");delay(1000);
}
else {
Serial.print("c");delay(1000);
}
if (value6==LOW){

Serial.print("i");delay(1000);
}
else {
Serial.print("z");delay(1000);
}

}

in my second try i worked with lists :



here is the code for the second one ( i worked with two sensors instead of 6) :

#include <SoftwareSerial.h>
const int IR1 = 4;
const int IR2 = 5;
int value1;
int value2;
void setup() {
Serial.begin(9600);
pinMode(IR1,INPUT);
pinMode(IR2,INPUT);
}
void loop() {
value1 = digitalRead(IR1);
value2= digitalRead(IR2);
delay(1000);
if (value1==LOW){

Serial.print("d");delay(1000);
Serial.print("/");delay(1000);

}
else {
Serial.print("n");delay(1000);
Serial.print("/");delay(1000);
}

if (value2==LOW){

Serial.print("e");delay(1000);
Serial.print("/");delay(1000);
}
else {
Serial.print("a");delay(1000);
Serial.print("/");delay(1000);
}
}

1 Like

Dear @meca_parking,
please verify if the BT setting on your app is waiting for a linefeed terminator character in the Design page. If so, your Arduino code must send a Linefeed to terminate the frame sending. In other words your last serial.print must be a serial.println() to be catched by the app.

More: if you use SoftwareSerial you shall instantiate the library before using it, by assigning two pins to the serial port : something like:
#include <SoftwareSerial.h>
SoftwareSerial myserial(12,13); // Rx and Tx pins

void setup() {
myserial.begin(9600);
}

PS how is your HW made ? If your IR sensors are active low, you would preferably use the INPUT_PULLUP setting when initializing the input ports. This will be useful to avoid to external pullup resistors for each IR sensor.

Give it a sight.
Cheers.

1 Like

I see you have 12 different single characters coming down the wire one after another (give or take a slash), each character setting a particular background color to a particular color.

Repeated logic like this can be condensed down into tables,

  • LabelDict: a table of 12 letters and their associated components (Labels)
  • ColorDict: a table of the same 12 letters and the associated background colors.

Each of those could be encoded in a global variable as a dictionary.

Given that, all you need to do with your input stream is to check each incoming letter against a dictionary. If found among the keys, look up the Label and Color value for that letter, and use the generic set any Label.BackgroundColor block against that Label and Color.

That would eliminate your if/then/else tree.

To traverse all the letters in incoming text, use the text segment block for a length of 1, for each number from 1 to length of the incoming text.

1 Like

is it possible to give us the MIT blocks ? or just an example , i really didn't understand what you've said ..... i don't know how to create 2 lists and combine them, ive tried everything and it hasnt worked so far, i need to get this projet done by tomorrow, my app does everyhting, it receives the data and i can display it using text in a label but when i try to use it to trigger something as simple as changing a background color it doesnt work. i just need an example where you create two lists and combine them to perform a common task, thank you so much in advance,.

Export your .aia file and upload it here.
export_and_upload_aia

1 Like

the project file :

multiple_IR_sensors.aia (6.2 KB)

the arduino code :

#include <SoftwareSerial.h>
const int IR1 = 4;
const int IR2 = 5;
int value1;
int value2;
void setup() {
Serial.begin(9600);
pinMode(IR1,INPUT);
pinMode(IR2,INPUT);
}
void loop() {
value1 = digitalRead(IR1);
value2= digitalRead(IR2);
delay(1000);
if (value1==LOW){

Serial.print("d");delay(1000);
Serial.print("/");delay(1000);

}
else {
Serial.print("n");delay(1000);
Serial.print("/");delay(1000);
}

if (value2==LOW){

Serial.print("e");delay(1000);
Serial.print("/");delay(1000);
}
else {
Serial.print("a");delay(1000);
Serial.print("/");delay(1000);
}
}

Hi @meca_parking,
it seems to me that you have too many delays in your Arduino code.
I would suggest you to remove all the delay(1000) between the characters sending and to leave only this one:
"
value2= digitalRead(IR2);
delay(1000);
if (value1==LOW){
"
Probably if you wait too much between sending characters, the AI2 receiving block exits before the end of frame.
Give it a sight (et bonne chance).

1 Like

This project should work with your 4 letter codes, and it should be clear how to extend this to more letters.





multiple_IR_sensors (1).aia (6.7 KB)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.