How to use the Serial Monitor Send button...?

Hi everyone. I've never used the send function in the Serial Monitor but I think I would like to. I have a sketch running which has a menu function which uses the variable "menu" to look at which page it should be on. The data is a simple int between 0 and 15.

I'm running the code on an arduino with the OLED plugged in but right now my board with the buttons on is not connected and I'm waiting for some parts to connect it properly. In the interim, am I able to send a command via the Serial monitor to change it manually? I tried sending "menu = 3;" but nothing happened... Any ideas?

You have to parse the data. The data arrives at your Arduino as a series of characters. This is not a debugger environments where you can read and write to memory.

https://www.arduino.cc/reference/tr/language/functions/communication/serial/read/

Ah so it can't be done then...?

Use the serial read function and then make your sketch do what you need it to do. In the simplest case just send a single character. Compare the character in a switch-case statement and send some data back or start some action. For emulating buttons you do not need more.

Hello
For purposes of tests I´m using the following code:
In the loop()

if (Serial.available()) keyboard();

and

void keyboard() {
static int modeAlt = mode;
  switch (Serial.read())
  {
    case '1': mode = One; break;
    case '2': mode = Two break;
    case '3': mode = Three; break;
    case '4': mode = Four; break;
  }
  if (modeAlt != mode) modeAlt = mode, Serial.println(modeTxt[mode]);
}
1 Like

Looks like paul was faster than me. Here is a quick example I just wrote based on BlinkWithoutDelay.

const int ledPin =  LED_BUILTIN;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 300;
bool blinkLedOn = true;

void setup()
{
  pinMode( ledPin, OUTPUT );

  Serial.begin( 9600 );
  while ( !Serial );
}

void loop()
{
  unsigned long currentMillis = millis();
  if ( Serial.available() > 0 )
  {
    char incomingByte = Serial.read();
    switch ( incomingByte )
    {
      case '1':
        blinkLedOn = true;
        break;
      case '0':
        blinkLedOn = false;
        break;
      default:
        break;
    }
  }

  if ( currentMillis - previousMillis >= interval )
  {
    previousMillis = currentMillis;
    if ( ledState == LOW && blinkLedOn )
    {
      ledState = HIGH;
    }
    else
    {
      ledState = LOW;
    }
    digitalWrite( ledPin, ledState );
  }
}

Brilliant, thanks very much that works great!

Hello
I´m using this addional case :slight_smile:

 case '#': Serial.println(F("\n\nR E S E T")); delay(2000); asm ("jmp 0"); break;

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