Arduino Transmitter and Reciever Help

Hello Arduino Community!

I have my arduino set up to transmit Morse code over AM at 1337Khz.
Pin 8 transmits. When a button on digitalpin 2 is on.

I also have it set up to recieve. My Walkman set at AM 1337 Khz is outputting audio to Analog 0.

Both work just fine individually. However, I am unable to read Analog 0 while I am broadcasting on pin 8.

It’s as if the microcontroller is executing all of the transmit code before it gets to the recieve code.
I want to transmit and recieve my transmission simultaneously.

Any help or different approaches are welcomed. Thanks!

long millisAtStart=0;
long millisAtEnd=0;

const long period_broadcast=8;

#define LENGTH_DIT 64

const int length_dit=LENGTH_DIT;
const int pause_dit=LENGTH_DIT;
const int length_dah=3*LENGTH_DIT;
const int pause_dah=LENGTH_DIT;
const int length_pause=7*LENGTH_DIT;

void dit(void);
void dah(void);
void pause(void);
void broadcast(int N_cycles);
void dontbroadcast(int N_cycles);

#define ASM_INC(reg) asm volatile ("inc %0" : "=r" (reg) : "0" (reg))

//AudioReading
const int analogInPin = A0;

//QuietButton
const int buttonPin = 2;
int buttonState = 0;

void setup()
{
  Serial.begin(9600);
  DDRB=0xFF;
  millisAtStart=millis();
  dit();
  millisAtEnd=millis();
  
  pinMode(buttonPin, INPUT);

  //Serial.print(millisAtEnd-millisAtStart);
  //Serial.print(" ");
  //Serial.print((length_dit+pause_dit)*period_broadcast*256/(millisAtEnd-millisAtStart)/2);
  //Serial.print("kHz ");
  //Serial.println();
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
  pattern();
  }
  Serial.println(analogRead(analogInPin));
  delay(100);
}

void pattern() {
  dah();
  dit();
  dah();
  dah();
  pause();
  dah();
  dit();
  dah();
  dah();
  pause();
  dah();
  dah();
  dit();
  dit();
  pause();
  pause();
}

void dit(void)
{
  for(int i=0;i<length_dit;i++)
  {
    broadcast(period_broadcast);
  }
  for(int i=0;i<pause_dit;i++)
  {
    dontbroadcast(period_broadcast);
  }
}

void dah(void)
{
  for(int i=0;i<length_dah;i++)
  {
    broadcast(period_broadcast);
  }
  for(int i=0;i<pause_dah;i++)
  {
    dontbroadcast(period_broadcast);
  }
}

void pause(void)
{
  for(int i=0;i<length_pause;i++)
  {
    dontbroadcast(period_broadcast);
  }
}

void broadcast(int N_cycles)
{
  unsigned int portvalue;
  for (int i=0;i<N_cycles;i++)
  {
    portvalue=0;
    do
    {
	PORTB=portvalue;
	ASM_INC(portvalue);
    }
    while(portvalue<255);
  }
}

void dontbroadcast(int N_cycles)
{
  unsigned int portvalue;
  PORTB=0x00;
  for (int i=0;i<N_cycles;i++)
  {
    portvalue=0;
    do
    {
	ASM_INC(portvalue);
	asm volatile ("NOP");
    }
    while(portvalue<255);
  }
}

I took your advice and I’m rewriting the code for millis. I actually found this code from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1170901754

I’ve gone as far as replacing the for(i=0,i<something,i++) with the millis. With that I can run the particular code for as long as I want rather than using the incrementing i.

I need help figuring out what this part of the code means:

// ### INC ### Increment Register (reg = reg + 1)
#define ASM_INC(reg) asm volatile ("inc %0" : "=r" (reg) : "0" (reg))

I know the above increments a register but I don’t know what for. How would I rewrite it so it’s not assembly loop.

ASM_INC(portvalue);
//add some assembly No OPerations to keep timing the same
asm volatile ("NOP");

I also don’t understand the above. What was the author trying to do there?

Thanks!

How can I increment a register and waste one clock cycle using the arduino language?

I’m confused on how this code works. I understand to achieve a frequency Pin 8 must output a squarewave by turning High then Low really fast.

PORTB=0x00 turns it low
PORTB=0xFF turns it high

So whats the need to increment registers? What does it accomplish and how can I do it outside of assembly.

Aww. I'll try an external AM transmitter.

Thank you very much for your help!