Thursday, February 22, 2024

arduino mega – Receives information from Mild sensor with ISR


I exploit a laser module (HW483) to transmit a collection of 0 or 1 information and Mild Sensor Photosensitive(LM393) makes use of ISR to obtain information and its digital pin as output. On the transmitter, I exploit the PyFirmata 2 to regulate my Arduino Mega 2560 and the transmitted information have as much as 21 steady portions of worth 1 or 0. On the receiver, I additionally use Arduino Mega 2560. The transmittion price is 1Kbps ,the acquired information works good. However when the speed is 2Kbps, the acquired information don’t have proper numbers.
Anybody have thought? Thanks lots

Ian (first time poster)
enter image description here

python transmitter code:

import time
from pyfirmata2 import ArduinoMega

board = ArduinoMega("/dev/ttyUSB0")
print("Hook up with ", board.identify)
blink = board.get_pin('d:3:o')
length = 500000    // 500000 ns/bit =500us/bit  //

selection = "00000111100011"  // enter transmitted information //
information=""
for merchandise in selection:
    information += merchandise
// 100000000000000000000001 is the beginning bits and finish bits //
information="100000000000000000000001"+information+'100000000000000000000001'
whereas (1):
    examine = enter("something")
    for merchandise in information:
        if merchandise == '1':
            start_time = time.monotonic_ns()
            whereas ((time.monotonic_ns()-start_time) <= length):
                blink.write(1)
                print('1')
        elif merchandise == '0':
            start_time = time.monotonic_ns()
            whereas ((time.monotonic_ns()-start_time) <= length):
                blink.write(0)
                print('0')
    blink.write(0)

python receiver code:

import serial
from time import sleep, time
import numpy as np

COM_PORT = "COM4"  
BAUD_RATES = 115200
ser = serial.Serial(COM_PORT, BAUD_RATES, timeout=1)  

attempt:
    information = ""
    num = 0
    recording = 0
    a = enter("Write something to start out ")
    print("Begin Obtain")
    whereas True:
        if ser.in_waiting:
            obtain = ser.readline().decode()
            receive1 = int(obtain)
            if receive1 >= 22:
                recording = recording + 1

            if recording == 1:
                if num % 2 == 1:
                    ingredient = "1"
                    for merchandise in vary(receive1):
                        information += ingredient
                        print("1")
                else:
                    ingredient = "0"
                    for merchandise in vary(receive1):
                        information += ingredient
                        print("0")
                num = num + 1
                break
            elif recording == 2:
                information = information[23:-1]

besides KeyboardInterrupt:
    ser.shut()  
    print("BYE!")

arduino receiver code:

const byte interrupt = 2; 
unstable boolean Potential = false;
unstable boolean a = false;
float Interval = 500;     //   500us/bit  //
unstable unsigned lengthy int Last_time = 0;
unstable int depend;

void setup() {
  attachInterrupt(digitalPinToInterrupt(interrupt), InterruptChange, CHANGE);
  Serial.start(115200);
}

void loop() {
  if (a == true) {
    Counting_Signal();
    Potential = !Potential;
    if (depend < 30) {
      Serial.print(depend);
      Serial.print("n");
    }
    a = false;
  }
}

void InterruptChange() {
  a = true;
}

void Counting_Signal() {
  depend = spherical((micros() - Last_time) / Interval);
  Last_time = micros();
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles