How to run loop after receiving command from serial

Hello guys,

I'm on a project now and faced problem and can't find a solution how to continue. Problem is I need to start sending data to Serial, when receiving command from Serial1. Everything looks working but I only manage to send one "line", instead of repeating all my needed lines. And can turn on my project as it should. It send one "char/line" and stops, but there must be 3 of them and always on repeat. Can anyone help me to fix this and that arduino will start sending it all the time? Thank you.

// Connect:
// Screen PIN 4 to Arduino PIN 2 (TX) 
// Screen PIN 7 to Arduino ground

enum display_mode_name {RTI_RGB, RTI_PAL, RTI_NTSC, RTI_OFF};
const char display_modes[] = {0x40, 0x45, 0x4C, 0x46};
const char brightness_levels[] = {0x20, 0x61, 0x62, 0x23, 0x64, 0x25, 0x26, 0x67, 0x68, 0x29, 0x2A, 0x2C, 0x6B, 0x6D, 0x6E, 0x2F};

int current_display_mode = RTI_NTSC;
//int current_display_mode = RTI_OFF;
bool send_brightness = true;
char current_brightness_level = 15;

//delay between bytes, ms
const int rti_delay = 100;

void setup() 
{
  Serial.begin(2400);
  Serial1.begin(9600);
}

void loop() {

         rtiWrite(display_modes[current_display_mode]); 
  
       if (send_brightness)
          rtiWrite(brightness_levels[current_brightness_level]);
       else
          rtiWrite(0x40);
    
  rtiWrite(0x83);
  }    

void rtiWrite(char byte) {
   if (Serial1.available())   {

    char ch = Serial1.read();
      if (ch == '1') {
      
      Serial.println(byte);
      delay(rti_delay);
      
    }
    } 
  }

you might try renaming the argument to rtiWrite() to something other than "byte" which is a typedef in Arduino.h. suggest "val"

i see byte is an acceptable symbol name

however, since you repeated call rtiWrite with different arguments, it's somewhat random which will see the input character.

of course it may make sense to verify that you can both correctly send to Serial and received from Serial1 (e.g. are the bit rates correct, why not just make them both the same).

if you're interested in processing various commands that mey either print something or just change something (e.g. brightness level) consider (you'll need to restore Serial1)

// Connect:
// Screen PIN 4 to Arduino PIN 2 (TX)
// Screen PIN 7 to Arduino ground

enum display_mode_name {RTI_RGB, RTI_PAL, RTI_NTSC, RTI_OFF};

const char display_modes[] = {0x40, 0x45, 0x4C, 0x46};
const char brightness_levels[] = {
    0x20, 0x61, 0x62, 0x23,  0x64, 0x25, 0x26, 0x67,
    0x68, 0x29, 0x2A, 0x2C,  0x6B, 0x6D, 0x6E, 0x2F
};

int current_display_mode     = RTI_NTSC;
int current_brightness_level = 15;

void setup ()
{
    Serial.begin (9600);
}

void loop () {
    if (! Serial.available ())
        return;

    switch (Serial.read ())  {
    case '3':
        Serial.println (display_modes[current_display_mode], HEX);
        break;
    case '2':
        Serial.println (brightness_levels[current_brightness_level], HEX);
        break;
    case '1':
        Serial.println (current_brightness_level);
        break;
    default:
        break;
    }
}

I'm maybe can't explain what I would like to get here. But I need to send 3 values to the screen, last value is always 0x83, otherwise screen won't turn on.
I'm sending from Raspberry (Serial1) value "1", which has to enable loop on Arduiino. In this point everything works except that Arduino sends over Serial (to the screen) only one value isnted of these threes, and if I send another "1" it send another value (this happens of random values). And I need that all three values should be sent after Arduino receives "1" trough Serial and Arduino should not stop sending this value. I need that this value from Serial1 "1" only ignites to start loop and if receives "0" stop send this loop or in best scenario sends other values (to shut down screen). :slight_smile:

so why don't you send 3 bytes of information ??

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