1

I was creating a project where the following part of the code reads certain area of the display(which i screen shared from mobile)

import cv2 as cv
import numpy as np
from PIL import ImageGrab
import serial
arduinodata = serial.Serial('com5',9600)
def led_on():
 arduinodata.write(b'1')
def led_off():
 arduinodata.write(b'0')
i=1
while True:
 screen = np.array(ImageGrab.grab(bbox=(0,0,1920,1080)))
 #define the screen resulation
 screen_res = 1920, 1080
 scale_width = screen_res[0] / screen.shape[1]
 scale_height = screen_res[1] / screen.shape[0]
 scale = min(scale_width, scale_height)
 #resized window width and height
 window_width = int(screen.shape[1] * scale)
 window_height = int(screen.shape[0] * scale)
 #cv.namedWindow('Resized Window', cv.WINDOW_NORMAL)
 cv.resizeWindow('Resized Window', window_width, window_height)
 resized = screen.resize()
 health_bar = screen[262:268, 142:365] 
 
 health_grayed = cv.cvtColor(health_bar, cv.COLOR_BGR2GRAY)
 count1 = cv.countNonZero(health_grayed)
if i==1:
 e =count1
 i=0
if e>count1:
 led_on()
 e=count1
if e<count1:
 
 e=count1

and whenever the value of the count1 variable decreases i want the Arduino led should be turned on

this was the arduino code

char serialData;
void setup() {
 // put your setup code here, to run once:
 pinMode(13,OUTPUT);
 digitalWrite(13,LOW);
 Serial.begin(9600);
}
void loop() 
 // put your main code here, to run repeatedly:
{
 if(Serial.available() >0)
 {
 digitalWrite(13,HIGH);
 delay(1000);
 digitalWrite(13,LOW);
 
 
 }
 
}

But the led is turning on but does not turns off . I don't know why

can anyone please help me

asked Jul 7, 2021 at 12:19

1 Answer 1

0

This happens because you are not reading from Serial.

When you send data to the Arduino, it gets placed in the Serial buffer. Serial.available() then returns the number of bytes in the buffer. The bytes are removed from the buffer by Serial.read()ing them, which you don't do. Since you don't remove any bytes from the buffer, Serial.available() will always return a number greater than zero from the moment on, that the first byte arrives. After the delay you turn the LED off via digitalWrite(13,LOW);, but in the next loop iteration the if statement is again executed (as described above). This happens so fast, that you think the LED just stays on. In reality it gets turned of for maybe a few microseconds and then turned on again.

You are sending a single byte for the LED to react. So it would be the simplest solution to just read a single byte from the buffer inside of the if statement. The result can be discarded.

void loop()
{
 if(Serial.available() >0)
 {
 digitalWrite(13,HIGH);
 delay(1000);
 digitalWrite(13,LOW);
 Serial.read(); 
 }
}

The Serial.read() removes one byte from the buffer and returns it (so it gets discarded here). This way the LED blinks for every byte, that the Arduino receives over Serial.


Note: Depending on how fast you are sending the data you might want to make more clear, that you have individual bytes. With the above code the LED will light up for 3 seconds straight if you send 3 bytes in (relatively) fast succession (meaning consecutive bytes less than a second apart). To make it more clear, that you have 3 individual bytes, you can add a delay() call after turning the LED off. That gives you the chance to actually see it turning off.

answered Jul 7, 2021 at 12:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.