Modulated High Frequency Signal

Hello Everybody,

I am currently working on a project using the arduino and could use some assistance. What I am trying to do is send a 400khz digital signal from one arduino to a second arduino. Not only am I needing to send the signal, but that signal also needs to be modulated and sent in 250us intervals. Furthermore these intervals will be determined by a user input(for a example byte of information). So if the user inputs 10001000, the arduino will take the first three zero’s and send 3 pulses(1 pulse = 400khz signal running for 250us), then when it gets to the 1, the output should be ‘0’ for 250us and so on. I posted the code below. It’s kinda long sorry :frowning: I can get the 400khz signal but I am having a hell of a time getting it to pulse in the increment I specify. Any help would be greatly appreciated. thanks!

#include <math.h>
#include <TimerThree.h>

// the setup routine runs once when you press reset:
void setup()
{
Serial.begin(115200); // open Serial ports
Serial1.begin(115200);
pinMode(51, OUTPUT); // pin 51 OUTPUT
digitalWrite(51, HIGH);
pinMode(11, OUTPUT);
//digitalWrite(11, LOW);
}

void loop() {
byte data_in; // data input from user
char binary_val[7]; // char array holding the 7 bits from input
int i = 0;
int j;
char input_buff[2]; // temporary input buffer
boolean data_resolved = false; // flags
char dump; // throw data into this variable to discard

Serial.print(“Please enter one byte of data(hex)\n\n”);

while(!data_resolved) {
if (Serial.available()){
delay(10);
if (Serial.available() == 2) {
input_buff* = Serial.read();*

  • input_buff[i+1] = Serial.read();*
    data_in = ascii_to_hex_byte(input_buff*,input_buff[i+1]);
    // binary_val = data_in_to_binary(byte data_in);
    _
    Serial.print("\nyour data value in HEX is\n\n");_
    Serial.print(data_in, HEX);
    _
    Serial.print("\n");_
    _
    Serial.print(“Your data value in binary is \n\n”);_
    Serial.print(data_in, BIN);
    _
    Serial.print("\n\n");*_

* for (i = 0; i < 8; i++) {*
binary_val = ((1 << i) & data_in) >> i;
* }*
* pinMode(11,OUTPUT);*
* TCCR1A = 0x80; // 400kHZ clock, pin 11*
* TCCR1B = 0x11;*
* OCR1A = 0x0A;*
* ICR1 = 0x14;*
* TCCR1A = 0x00;*

* write(binary_val);
data_resolved = true;
_ }
else {
Serial.print(“Invalid Data Packet. Please enter a one byte data packet(hex)”);
while(Serial.available()) {
dump = Serial.read();
dump = 0;
}
}
}
}
}*_

char ascii_to_hex_byte(char c1, char c2){//takes 2 ascii characters and returns 1 byte in hex
* char res[2] ={0};//single digit decode results*
* char finRes = 0;//final result*
* char inChar[2] = {c1,c2};//inputs*
* int i; // iterator*

* for (i=0; i<2; i++){*
if(inChar>=48 && inChar<=57) res = inChar*-48;*
else if(inChar>=65 && inChar<=70) res = inChar - 65 + 0xa;
else if(inChar>=97 && inChar<=102) res = inChar - 97 + 0xa;
* else{*
* Serial.print(“Error: Invalid Ascii to Hex Conversion\n”);*
* }*
* }*

* res[0]=res[0]<<4;//shift the first decode to the left for first hex digit*
* finRes=res[0]+res[1];//append the second hex digit*
* return finRes;*
}
int write(char binary_val[]) {
* int i = 0;*
* int j = 0;*
* byte packet[8];*
// byte preamble[8];
* //byte postamble[8];*
* boolean data_ready = false;
boolean car_ready = false;*

* for (j = 0; j < 8; j++) {*
* packet[j] = binary_val[j];
_ }*_

* while(!data_ready){
_ for (i = 0; i < 8; i++) {
Serial.print(“packet[”);
Serial.print(i), DEC;
Serial.print("] = “);
Serial.print(packet, BIN);
Serial.print(”\n\n");_

data_ready = true;
_ }
}*_

* for (i = 0; i < 8; i++){*
_ Serial1.write(packet*);
Serial.print("\n");
}
for (i = 0; i < 5; i++) {
if (packet == 0){
Timer3.initialize(250);
TCCR1A = 0x80;_

Timer3.attachInterrupt(stop_signal);*

* }*
_ else if (packet == 1){
* Timer3.initialize(250);
TCCR1A = 0x00;_

Timer3.attachInterrupt(start_signal);
_ }
}
}*_

void stop_signal(void){

* TCCR1A = 0; *
return;

}
void start_signal(void){

* TCCR1A = 0x80;*

return;

}

Do you mind if I ask what your trying to accomplish? This seems to be an overly complicated method to send digital signals between two Arduinos.

It is somewhat similiar to the way IR remote codes are transmitted; however, that usually has both a much lower base frequency (36-40 kHz) and also typically use devices that filter that base frequency out and only provide the 1's and 0's.

By the way, please enclose your code in the code /code tags (the # sign button in the edit bar). It makes it easier for others to read and is part of the forum rules.

Sorry about that, I just signed up and wasn't aware of the rules! The reason for this project is I am currently working as an intern over the summer at a company in my area. They have a new electronic device that will be released at some time in the future and part of my intern project is simulating this device(and it's external programmer) through the use of two arduinos. The device will read a '0' for every 250us interval where a 400khz signal is present and a '1' when there is no 400khz signal. thanks for the help.