Go Down

Topic: Simple Terminal Program Has a Problem (Read 16041 times) previous topic - next topic

goodinventor

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?

KenF

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

Does this get you any closer to your aspirations.
Code: [Select]

//
// 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;
  }
}


goodinventor

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.

Robin2

#3
Dec 29, 2014, 02:40 pm Last Edit: Dec 29, 2014, 02:42 pm by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

goodinventor

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?

UKHeliBob

Quote
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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

goodinventor

OK. Attached is the program so far.

Any commands I should add that would be useful?

AWOL

Quote
Attached is the program so far.
It's small enough for you to have posted it for everyone's convenience.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

goodinventor


goodinventor

Now what could I add to the program?

Delta_G

|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

KenF


goodinventor

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


Go Up