I would love to receive help with frequency transmission using Arduino

Glad you noticed! How long do you suppose this line in your posted code delays?

delay(0.00025); // wait for a second
1 Like

That's the reason I didn't send the code At first, I knew it was impossible to make a delay of less than one millisecond and that's why it would have seemed unnecessary to me to send a code that was clearly wrong.
But I couldn't explain myself well, I was hoping that if I just send a wrong code you would correct me

Well you FINALLY DID post the code, and you learned about delayMicroseconds()!

Imagine how much of your and everyone else's time was wasted by not following the forum guidelines.

1 Like

I'm new to the forums and this is my first post, I thought the code wouldn't help because it's wrong but I agree I should have posted it from the start and explained why I think it's wrong.

1 Like

Next time I will post it from the beginning

1 Like

So use a LASER to send morse code just a matter of encrypt and decrypt on each end. Modulate your laser light and do same with modulate and demodulate.

Morse code is easy enough and can be done using an Arduino Uno R3 just as an example.

Ron

I didn't look at it that way, but you don't have to attack and say I'm hiding things. Just write me, send the code. I don't know the procedures here and apologize if I didn't explain things well. But your attitude is very aggressive

Your detector does not need to re-calculate interim Hightime until the HIGH actually finishes. This would do it cleaner:

    while ( digitalRead(digitalPin) == HIGH); // wait to fall
    Hightime = micros() - T0; //measure the high time;

I cleaned up a few things in your code to streamline the edge-detection polling and added using the TimerOne library to simulate a transmission:

// code modified from https://forum.arduino.cc/t/i-would-love-to-receive-help-with-frequency-transmission-using-arduino/1211551/29?u=davex
// https://wokwi.com/projects/387127367989449729

unsigned long T0;
float Hightime = 0;
float Lowtime = 0;
float frequency;
float Time;
float Duty;
float digitalPin = 7;
const byte simPin = 9;

#include <TimerOne.h>

void setup() {
  Serial.begin(115200);
  pinMode(digitalPin, INPUT);
  pinMode(9, OUTPUT);
  Timer1.initialize(250);  // 250 us = 4 kHz
  //analogWrite(simPin, 10);
  Timer1.pwm(simPin, 500);
}

void loop() {
  bool read = digitalRead(digitalPin);
  for (int simDuty = 1023 / 10; simDuty < 1023; simDuty += 1023 / 5) {
    while (digitalRead(digitalPin) == LOW); // wait to start
    T0 =  micros(); // mark the first rising
    while ( digitalRead(digitalPin) == HIGH); // wait to fall
    Hightime = micros() - T0; //measure the high time;
    while ( digitalRead(digitalPin) == LOW);
    Time = micros() - T0; // measure the total time

    frequency = 1000000 / Time ;
    Duty = Hightime / Time;
    Serial.print("Tus:");
    Serial.print(Time);
    Serial.print(" f:");
    Serial.print(frequency);
    Serial.print(" HighUs:");
    Serial.print(Hightime);
    Serial.print(" d:");
    Serial.println(Duty, 3);
    Timer1.pwm(simPin, simDuty); // change the duty cycle sim
    delay(777); // pause for viewing
    while (digitalRead(digitalPin) == HIGH); // wait again
  }
}

thank you very much
By the way, is there a reason why the code won't work on the mega 2560? It works fine on uno but on mega it doesn't run

Timer one only works on timer 1 pins , which differ from device to device. There’s a table in the docs that says which pins work:

https://www.pjrc.com/teensy/td_libs_TimerOne.html

1 Like

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