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
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;
}