Simple Terminal Program Has a Problem

Hello,

Attached is the program so far.

The problem is that when I type 'r' or 's', the Arduino does not respond via serial. Can you all show me how to fix it?

Unos_0_0_1.ino (597 Bytes)

Your Serial.read belongs INSIDE your loop function. Not at the top of your sketch.

Does this get you any closer to your aspirations.

//
// Unos_0_0_1
//
// programmed by George Andrews
//
// (c. 2014)
//

// input

void setup()
{
  Serial.begin(9600);
  
  Serial.println("Welcome...");
  delay(3000);
  
  Serial.println("root@unos:~#");
  delay(3000);
  
  Serial.print("root@unos:");
}

void loop()
{
char input = Serial.read();

  switch (input)
  {
    case 's':
      Serial.println(" ");
      Serial.println("Shutting down...");
      delay(3000);
      break;
      
    case 'r':
      Serial.println(" ");
      Serial.println("Restarting...");
      Serial.println("Welcome...");
      delay(3000);
      break;
  }
}

Yes, it now does everything I want it to. Thank you, KenF. Now how do I implement strings for Serial.read() in the switch control structure? When I tried it, it generated errors.

goodinventor:
Now how do I implement strings for Serial.read() in the switch control structure? When I tried it, it generated errors.

You need to post your revised code so we know what you are now dealing with.

Also have a look at serial input basics [Edit to add] - which uses strings (small s) [/edit]

...R

When I replace 'r' with "reboot" and 's' with "shutdown", the compiler generates an error. When I replace 'r' with 'r' && 'e' && 'b' && 'o' && 'o' && 't', the compiler generates an error for duplicated characters. What should I do?

Now how do I implement strings for Serial.read() in the switch control structure?

switch/case uses the value of a variable as the switch parameter so it cannot use strings. You might consider using strcmp() to test strings against literal strings or string variables.

OK. Attached is the program so far.

Any commands I should add that would be useful?

Unos_0_0_1.ino (1.16 KB)

Attached is the program so far.

It's small enough for you to have posted it for everyone's convenience.

goodinventor:
Any commands I should add that would be useful?

Did you look at the link I posted in Reply #4 which probably covers everything you need.

...R

Yes, I did. Thank you.

Now what could I add to the program?

A blinking light.

Delta_G:
A blinking light.

Without delay :slight_smile:

OK. I added a blink LED command. Attached is the new and improved program.

Unos_0_0_1.ino (1.46 KB)

See reply #8

Yes, I did. Thank you for reminding me.

Are there any more improvements I can make?

goodinventor:
Yes, I did. Thank you for reminding me.

But you chose to ignore.

Well, I didn't think that the code block could hold code that long. I thought it should be used for snippets.

goodinventor:
Well, I didn't think that the code block could hold code that long. I thought it should be used for snippets.

No, snippets are what we don't want to see.