programming problem

Hi!

I downloaded project from "Krasutski". I added audio isolation because the speakers were buzzing when the engine was running and the phone was charging. (Ground loop). Here is a link to a quick "schematic".
I soldered phone ground to 14. pin on Adruino and microphone signal to 16. pin. When I want to "push microphone button" (connect microphone pin to phone ground), it just connects 14. pin to car ground (digitalWrite(14, 0)) and then 16. pin to ground as well (digitalWrite(16, 0)). It works in loop function but I need it to work when I push some button on my radio -> in "process_radio_message" function. There it doesnt work... and I really dont know why. It is just some programming problem, but I cant solve this. Please, can you help me?

Here is program with my edit:

tape_emulator_jack_3_5.ino (11.8 KB)

  noInterrupts(); {

    do {
      delay(10);
    } while (digitalRead(TYPE_IO_PIN) != HIGH);

    detachInterrupt(digitalPinToInterrupt(TYPE_IO_PIN));
    digitalWrite(TYPE_IO_PIN, HIGH);
    pinMode(TYPE_IO_PIN, OUTPUT);

    for (uint8_t i = 0; i < lenght; i++) {
      send_nibble(message[i]);
    }

    pinMode(TYPE_IO_PIN, TYPE_IO_PIN_INPUT_MODE);
    attachInterrupt(digitalPinToInterrupt(TYPE_IO_PIN), collectInputData, CHANGE);

  } interrupts();

delay() does not work with interrupts disabled.

Serial.print() should NOT be used when interrupts are disabled.

Most of your code happens with interrupts disabled. You will need to restructure your code so that interrupts are enabled except when they absolutely need to be disabled.

PaulS:
delay() does not work with interrupts disabled.

not enabled?

Most of this code executes in a nointerrupts block, which is a problem. It's necessary to do this to stop conflicts - the interrupt overwriting the buffer while the loop is processing it. However, many arduino things (Serial, delay()) won't work in a nointerrupts block because they themselves use interrupts to function.

Your second problem is that process_radio_message invokes send_message, which itself disables and re-enables interrupts.

To fix this, the loop() needs to enter a nointerrupts block, copy the shared data into a separate buffer, clear the shared buffers, and then re-enable interrupts. The loop code can then proceed by processing the copied data.

Something like:

  if ( ( millis() - rx_time_ms ) > RX_TIMEOUT_MS) {
    noInterrupts();

    byte nibblesReceivedCopy = nibblesReceived;
    uint8_t inNibblesBufferCopy[IN_BUFFER_SIZE];
    if (nibblesReceivedCopy != 0U ) {
      memcpy(inNibblesBufferCopy, inNibblesBuffer, IN_BUFFER_SIZE);
      bufferReset();
    }
    interrupts();

    if(nibblesReceivedCopy != 0U) {
        DEBUG_PRINT("RX[");
        DEBUG_PRINT(nibblesReceivedCopy);
        DEBUG_PRINT("] ");

        for (int i = 0; i < nibblesReceivedCopy; i++) {
          DEBUG_PRINT(inNibblesBufferCopy[i], HEX);
        }
        DEBUG_PRINT("\r\n");

        process_radio_message((rxMessage_t*)inNibblesBufferCopy);

        rx_time_ms = millis();
      }
    }
  }

It's important that process_radio_message only use the parameters passed into it and not try to access the shared buffer, because while it is running that shared buffer might be modified.