Go Down

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

goodinventor

OK. I modified the code to implement strings and use millis(). It compiled just fine, but when I opened up the serial monitor, it only displayed 'Welcome...'. Below is the code.

Code: [Select]

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

int LED_PIN = 13; // LED pin
int LED_PIN_STATE = LOW; // initial state of LED pin
long LAST_LED_UPDATE = 0; // time of last LED update
unsigned long LED_BLINK_INTERVAL = 500; // time interval for LED to blink
unsigned long TIME_NEEDED;
boolean NewData = false;
char inputChar; // input character
char input[8]; // input buffer

int Time(unsigned long TIME_NEEDED)
{
  unsigned long INTERVAL = TIME_NEEDED;
  long LAST_UPDATE = 0;
  unsigned long CURRENT_TIME = millis();
 
  if (CURRENT_TIME - LAST_UPDATE > INTERVAL)
  {
    LAST_UPDATE = CURRENT_TIME;
   
    return 1; // signify that the time is up
  }
}

void ReceiveSerialStringWithEndmarker()
{
  static byte ndx = 0;
  char Endmarker = '\n';
  int NumChars = 8;
 
  if (Serial.available() > 0)
  {
    if (inputChar != Endmarker)
    {
      input[ndx] = inputChar;
      ndx++;
     
      if (ndx >= NumChars)
      {
        ndx = NumChars - 1;
      }
    }
    else
    {
      input[ndx] = '\0'; // end string
      ndx = 0;
      NewData = true;
    }
  }
}

void setup()
{
  Serial.begin(9600);
 
  pinMode(LED_PIN, OUTPUT); // LED pin
 
  Serial.println("Welcome...");
  TIME_NEEDED = 3;
  Time(TIME_NEEDED);
 
  if (Time(TIME_NEEDED) == 1)
  {
    Serial.println("root@unos:~#");
  }
 
  TIME_NEEDED = 3;
  Time(TIME_NEEDED);
 
  if (Time(TIME_NEEDED) == 1)
  {
    Serial.print("root@unos:");
  }
 
  interrupts();
}

void loop()
{
  // input character
  char inputChar = Serial.read();
 
  char input[8];
 
  unsigned long time = millis();
 
  ReceiveSerialStringWithEndmarker();
 
  if (input[0] == 's' && input[1] == 'h' && input[2] == 'u' && input[3] == 't' && input[4] == 'd' && input[5] == 'o' && input[6] == 'w' && input[7] == 'n')
  {
    Serial.print(input[0]);
    Serial.print(input[1]);
    Serial.print(input[2]);
    Serial.print(input[3]);
    Serial.print(input[4]);
    Serial.print(input[5]);
    Serial.print(input[6]);
    Serial.print(input[7]);
    Serial.println(input[8]);
     
    Serial.println("Shutting down...");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
   
    if (Time(TIME_NEEDED) == 1)
    {
      noInterrupts();
    }
  }
  else if (input[0] == 'r' && input[1] == 'e' && input[2] == 's' && input[3] == 't' && input[4] == 'a' && input[5] == 'r' && input[6] == 't')
  {
    Serial.print(input[0]);
    Serial.print(input[1]);
    Serial.print(input[2]);
    Serial.print(input[3]);
    Serial.print(input[4]);
    Serial.print(input[5]);
    Serial.print(input[6]);
    Serial.print(input[7]);
    Serial.println(input[8]);
     
    Serial.print("Restarting...");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
     
    if (Time(TIME_NEEDED) == 1)
    {
      noInterrupts();
    }
     
    interrupts();
     
    Serial.println("Welcome...");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
     
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.println("root@unos:~#");
    }
     
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
     
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.print("root@unos:");
    }
  }
  else if (input[0] = 'p' && input[1] == 'r' && input[2] == 'i' && input[3] == 'n' && input[4] == 't' && input[5] == ' ')
  {
    Serial.print(input[0]);
    Serial.print(input[1]);
    Serial.print(input[2]);
    Serial.print(input[3]);
    Serial.print(input[4]);
    Serial.print(input[5]);
    Serial.print(input[6]);
    Serial.print(input[7]);
    Serial.println(input[8]);
   
    Serial.println(input[6]);
   
    Serial.println("root@unos:~#");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
     
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.print("root@unos:");
    }
  }
  else if (input[0] == 'b' && input[1] == 'l' && input[2] == 'i' && input[3] == 'n' && input[4] == 'k')
  {
    Serial.print(input[0]);
    Serial.print(input[1]);
    Serial.print(input[2]);
    Serial.print(input[3]);
    Serial.print(input[4]);
    Serial.print(input[5]);
    Serial.print(input[6]);
    Serial.print(input[7]);
    Serial.println(input[8]);
   
    if (time - LAST_LED_UPDATE > LED_BLINK_INTERVAL)
    {
      LAST_LED_UPDATE = time;
       
      if (LED_PIN_STATE == LOW)
      {
        LED_PIN_STATE = HIGH;
      }
      else
      {
        LED_PIN_STATE = LOW;
      }
       
      digitalWrite(LED_PIN, LED_PIN_STATE);
    }
     
    Serial.println("root@unos:~#");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
     
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.print("root@unos:");
    }
  }
}


Why does it only show 'Welcome...'?

UKHeliBob

In this function
Code: [Select]
int Time(unsigned long TIME_NEEDED)
{
  unsigned long INTERVAL = TIME_NEEDED;
  long LAST_UPDATE = 0;
  unsigned long CURRENT_TIME = millis();
  
  if (CURRENT_TIME - LAST_UPDATE > INTERVAL)
  {
    LAST_UPDATE = CURRENT_TIME;
    
    return 1; // signify that the time is up
  }
}

You declare LAST_UPDATE and initialise it to zero.
Why ?

Later in the function you set it to the current time but guess what happens to it the next time the function is called ?
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. Now the program won't respond to my input. Below is the code.

Code: [Select]

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

int LED_PIN = 13; // LED pin
int LED_PIN_STATE = LOW; // initial state of LED pin
long LAST_LED_UPDATE = 0; // time of last LED update
unsigned long LED_BLINK_INTERVAL = 500; // time interval for LED to blink
unsigned long TIME_NEEDED;
boolean NewData = false;
char inputChar; // input character
char input[8]; // input buffer
unsigned long CURRENT_TIME = millis();
long LAST_UPDATE = CURRENT_TIME;

int Time(unsigned long TIME_NEEDED)
{
  unsigned long INTERVAL = TIME_NEEDED;
 
  if (CURRENT_TIME - LAST_UPDATE > INTERVAL)
  {
    LAST_UPDATE = CURRENT_TIME;
   
    return 1; // signify that the time is up
  }
}

void ReceiveSerialStringWithEndmarker()
{
  static byte ndx = 0;
  char Endmarker = '\n';
  int NumChars = 8;
 
  if (Serial.available() > 0)
  {
    if (inputChar != Endmarker)
    {
      input[ndx] = inputChar;
      ndx++;
     
      if (ndx >= NumChars)
      {
        ndx = NumChars - 1;
      }
    }
    else
    {
      input[ndx] = '\0'; // end string
      ndx = 0;
      NewData = true;
    }
  }
}

void setup()
{
  Serial.begin(9600);
 
  pinMode(LED_PIN, OUTPUT); // LED pin
 
  Serial.println("Welcome...");
  TIME_NEEDED = 3;
  Time(TIME_NEEDED);
 
  if (Time(TIME_NEEDED) == 1)
  {
    Serial.println("root@unos:~#");
  }
 
  TIME_NEEDED = 3;
  Time(TIME_NEEDED);
 
  if (Time(TIME_NEEDED) == 1)
  {
    Serial.print("root@unos:");
  }
 
  interrupts();
}

void loop()
{
  // input character
  char inputChar = Serial.read();
 
  char input[8];
 
  unsigned long time = millis();
 
  ReceiveSerialStringWithEndmarker();
 
  if (input[0] == 's' && input[1] == 'h' && input[2] == 'u' && input[3] == 't' && input[4] == 'd' && input[5] == 'o' && input[6] == 'w' && input[7] == 'n')
  {
    Serial.print(input[0]);
    Serial.print(input[1]);
    Serial.print(input[2]);
    Serial.print(input[3]);
    Serial.print(input[4]);
    Serial.print(input[5]);
    Serial.print(input[6]);
    Serial.print(input[7]);
    Serial.println(input[8]);
     
    Serial.println("Shutting down...");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
   
    if (Time(TIME_NEEDED) == 1)
    {
      noInterrupts();
    }
  }
  else if (input[0] == 'r' && input[1] == 'e' && input[2] == 's' && input[3] == 't' && input[4] == 'a' && input[5] == 'r' && input[6] == 't')
  {
    Serial.print(input[0]);
    Serial.print(input[1]);
    Serial.print(input[2]);
    Serial.print(input[3]);
    Serial.print(input[4]);
    Serial.print(input[5]);
    Serial.print(input[6]);
    Serial.print(input[7]);
    Serial.println(input[8]);
     
    Serial.print("Restarting...");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
     
    if (Time(TIME_NEEDED) == 1)
    {
      noInterrupts();
    }
     
    interrupts();
     
    Serial.println("Welcome...");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
     
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.println("root@unos:~#");
    }
     
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
     
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.print("root@unos:");
    }
  }
  else if (input[0] = 'p' && input[1] == 'r' && input[2] == 'i' && input[3] == 'n' && input[4] == 't' && input[5] == ' ')
  {
    Serial.print(input[0]);
    Serial.print(input[1]);
    Serial.print(input[2]);
    Serial.print(input[3]);
    Serial.print(input[4]);
    Serial.print(input[5]);
    Serial.print(input[6]);
    Serial.print(input[7]);
    Serial.println(input[8]);
   
    Serial.println(input[6]);
   
    Serial.println("root@unos:~#");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
     
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.print("root@unos:");
    }
  }
  else if (input[0] == 'b' && input[1] == 'l' && input[2] == 'i' && input[3] == 'n' && input[4] == 'k')
  {
    Serial.print(input[0]);
    Serial.print(input[1]);
    Serial.print(input[2]);
    Serial.print(input[3]);
    Serial.print(input[4]);
    Serial.print(input[5]);
    Serial.print(input[6]);
    Serial.print(input[7]);
    Serial.println(input[8]);
   
    if (time - LAST_LED_UPDATE > LED_BLINK_INTERVAL)
    {
      LAST_LED_UPDATE = time;
       
      if (LED_PIN_STATE == LOW)
      {
        LED_PIN_STATE = HIGH;
      }
      else
      {
        LED_PIN_STATE = LOW;
      }
       
      digitalWrite(LED_PIN, LED_PIN_STATE);
    }
     
    Serial.println("root@unos:~#");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
     
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.print("root@unos:");
    }
  }
}


How can I fix the problem?

UKHeliBob

Try printing each character as it is read in the ReceiveSerialStringWithEndmarker() function.  Are you getting anything and is it what you expect ?

By the way.  You don't need to do this
Code: [Select]
   Serial.print(input[0]);
    Serial.print(input[1]);
    Serial.print(input[2]);
    Serial.print(input[3]);
    Serial.print(input[4]);
    Serial.print(input[5]);
    Serial.print(input[6]);
    Serial.print(input[7]);
    Serial.println(input[8]);

You can just do
Code: [Select]
Serial.println(input);
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

AWOL

#34
Jan 01, 2015, 06:22 pm Last Edit: Jan 01, 2015, 06:24 pm by AWOL
Try printing each character as it is read in the ReceiveSerialStringWithEndmarker() function.  Are you getting anything and is it what you expect ?

By the way.  You don't need to do this
Code: [Select]
   Serial.print(input[0]);
    Serial.print(input[1]);
    Serial.print(input[2]);
    Serial.print(input[3]);
    Serial.print(input[4]);
    Serial.print(input[5]);
    Serial.print(input[6]);
    Serial.print(input[7]);
    Serial.println(input[8]);

You can just do
Code: [Select]
Serial.println(input);

. . . but only if the string is correctly terminated.
It doesn't look llike it is.

BTW, you don't own "input[8]", so you shouldn't try to print it.

UKHeliBob

Quote
. . . but only if the string is correctly terminated.
It doesn't look llike it is.
But at least there was an attempt to terminate it properly.
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. I made the changes. It still doesn't respond to input, though. Here is the code.

Code: [Select]

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

int LED_PIN = 13; // LED pin
int LED_PIN_STATE = LOW; // initial state of LED pin
long LAST_LED_UPDATE = 0; // time of last LED update
unsigned long LED_BLINK_INTERVAL = 500; // time interval for LED to blink
unsigned long TIME_NEEDED;
boolean NewData = false;
char inputChar; // input character
char input[8]; // input buffer
unsigned long CURRENT_TIME = millis();
long LAST_UPDATE = CURRENT_TIME;

int Time(unsigned long TIME_NEEDED)
{
  unsigned long INTERVAL = TIME_NEEDED;
  
  if (CURRENT_TIME - LAST_UPDATE > INTERVAL)
  {
    LAST_UPDATE = CURRENT_TIME;
    
    return 1; // signify that the time is up
  }
}

void ReceiveSerialStringWithEndmarker()
{
  static byte ndx = 0;
  char Endmarker = '\n';
  int NumChars = 8;
  
  if (Serial.available() > 0)
  {
    if (inputChar != Endmarker)
    {
      input[ndx] = inputChar;
      Serial.print(input[ndx]);
      ndx++;
      
      if (ndx >= NumChars)
      {
        ndx = NumChars - 1;
      }
    }
    else
    {
      input[ndx] = '\0'; // end string
      ndx = 0;
      NewData = true;
    }
  }
}

void setup()
{
  Serial.begin(9600);
  
  pinMode(LED_PIN, OUTPUT); // LED pin
  
  Serial.println("Welcome...");
  TIME_NEEDED = 3;
  Time(TIME_NEEDED);
  
  if (Time(TIME_NEEDED) == 1)
  {
    Serial.println("root@unos:~#");
  }
  
  TIME_NEEDED = 3;
  Time(TIME_NEEDED);
  
  if (Time(TIME_NEEDED) == 1)
  {
    Serial.print("root@unos:");
  }
  
  interrupts();
}

void loop()
{
  // input character
  char inputChar = Serial.read();
  
  char input[8];
  
  unsigned long time = millis();
  
  ReceiveSerialStringWithEndmarker();
  
  if (input[0] == 's' && input[1] == 'h' && input[2] == 'u' && input[3] == 't' && input[4] == 'd' && input[5] == 'o' && input[6] == 'w' && input[7] == 'n')
  {
    Serial.println(input);
      
    Serial.println("Shutting down...");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
    
    if (Time(TIME_NEEDED) == 1)
    {
      noInterrupts();
    }
  }
  else if (input[0] == 'r' && input[1] == 'e' && input[2] == 's' && input[3] == 't' && input[4] == 'a' && input[5] == 'r' && input[6] == 't')
  {
    Serial.println(input);
      
    Serial.print("Restarting...");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
      
    if (Time(TIME_NEEDED) == 1)
    {
      noInterrupts();
    }
      
    interrupts();
      
    Serial.println("Welcome...");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
      
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.println("root@unos:~#");
    }
      
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
      
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.print("root@unos:");
    }
  }
  else if (input[0] = 'p' && input[1] == 'r' && input[2] == 'i' && input[3] == 'n' && input[4] == 't' && input[5] == ' ')
  {
    Serial.println(input);
    
    Serial.println(input[6]);
    
    Serial.println("root@unos:~#");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
      
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.print("root@unos:");
    }
  }
  else if (input[0] == 'b' && input[1] == 'l' && input[2] == 'i' && input[3] == 'n' && input[4] == 'k')
  {
    Serial.println(input);
    
    if (time - LAST_LED_UPDATE > LED_BLINK_INTERVAL)
    {
      LAST_LED_UPDATE = time;
        
      if (LED_PIN_STATE == LOW)
      {
        LED_PIN_STATE = HIGH;
      }
      else
      {
        LED_PIN_STATE = LOW;
      }
        
      digitalWrite(LED_PIN, LED_PIN_STATE);
    }
      
    Serial.println("root@unos:~#");
    TIME_NEEDED = 3;
    Time(TIME_NEEDED);
      
    if (Time(TIME_NEEDED) == 1)
    {
      Serial.print("root@unos:");
    }
  }
}


What do I need to change in the program to make it respond to input?

KenF

What do I need to change in the program to make it respond to input?
It's your Time function.  Apart from the issue I've already tried to get into your head, the value returned by an integer function that does not specify a return value is undefined.  Might be zero it might not.

goodinventor

I modified the code to use delay() instead of millis() to see if it would respond to input then. But it did not. Now I know there is a problem with the algorithm for reading strings from serial. Can you see any problems? How can I fix them?

AWOL

You've got a global "input" and a local "input" in "loop()"

That's a problem for you, I think.

goodinventor

OK. I fixed that problem. But it still doesn't respond to input. Here is the code.

Code: [Select]

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

int LED_PIN = 13; // LED pin
int LED_PIN_STATE = LOW; // initial state of LED pin
long LAST_LED_UPDATE = 0; // time of last LED update
unsigned long LED_BLINK_INTERVAL = 500; // time interval for LED to blink
unsigned long TIME_NEEDED = 0;
boolean NewData = false;
char inputChar; // input character
char input[8]; // input buffer
unsigned long CURRENT_TIME = millis();
long LAST_UPDATE = CURRENT_TIME;

int Time(unsigned long TIME_NEEDED)
{
  unsigned long INTERVAL = TIME_NEEDED;
 
  if (CURRENT_TIME - LAST_UPDATE > INTERVAL)
  {
    LAST_UPDATE = CURRENT_TIME;
   
    return 1; // signify that the time is up
  }
 
  return 0;
}

void ReceiveSerialStringWithEndmarker()
{
  static byte ndx = 0;
  char Endmarker = '\n';
  int NumChars = 8;
 
  if (Serial.available() > 0)
  {
    if (inputChar != Endmarker)
    {
      input[ndx] = inputChar;
      Serial.print(input[ndx]);
      ndx++;
     
      if (ndx >= NumChars)
      {
        ndx = NumChars - 1;
      }
    }
    else
    {
      input[ndx] = '\0'; // end string
      ndx = 0;
      NewData = true;
    }
  }
}

void setup()
{
  Serial.begin(9600);
 
  pinMode(LED_PIN, OUTPUT); // LED pin
 
  Serial.println("Welcome...");
  delay(3);
  Serial.println("root@unos:~#");
  delay(3);
  Serial.print("root@unos:");
 
  interrupts();
}

void loop()
{
  // input character
  char inputChar = Serial.read();
 
  unsigned long time = millis();
 
  ReceiveSerialStringWithEndmarker();
 
  if (input[0] == 's' && input[1] == 'h' && input[2] == 'u' && input[3] == 't' && input[4] == 'd' && input[5] == 'o' && input[6] == 'w' && input[7] == 'n')
  {
    Serial.println(input);
     
    Serial.println("Shutting down...");
    delay(3);
    noInterrupts();
  }
  else if (input[0] == 'r' && input[1] == 'e' && input[2] == 's' && input[3] == 't' && input[4] == 'a' && input[5] == 'r' && input[6] == 't')
  {
    Serial.println(input);
   
    Serial.print("Restarting...");
    delay(3);
   
    noInterrupts();
   
    interrupts();
   
    Serial.println("Welcome...");
    delay(3);
    Serial.println("root@unos:~#");
    delay(3);
    Serial.print("root@unos:");
  }
  else if (input[0] = 'p' && input[1] == 'r' && input[2] == 'i' && input[3] == 'n' && input[4] == 't' && input[5] == ' ')
  {
    Serial.println(input);
   
    Serial.println(input[6]);
   
    Serial.println("root@unos:~#");
    delay(3);
    Serial.print("root@unos:");
  }
  else if (input[0] == 'b' && input[1] == 'l' && input[2] == 'i' && input[3] == 'n' && input[4] == 'k')
  {
    Serial.println(input);
   
    if (time - LAST_LED_UPDATE > LED_BLINK_INTERVAL)
    {
      LAST_LED_UPDATE = time;
     
      if (LED_PIN_STATE == LOW)
      {
        LED_PIN_STATE = HIGH;
      }
      else
      {
        LED_PIN_STATE = LOW;
      }
     
      digitalWrite(LED_PIN, LED_PIN_STATE);
    }
     
    Serial.println("root@unos:~#");
    delay(3);
    Serial.print("root@unos:");
  }
}


Are there any other problems you see with this code?

AWOL

#41
Jan 02, 2015, 10:51 pm Last Edit: Jan 02, 2015, 10:56 pm by AWOL
What does your debug output tell you is going on?

Swallowing a character every time through "loop()" for no very good reason doesn't seem sensible.

goodinventor

There are no debug problems that I can see. The serial obviously works because I can see the output from the Arduino. The problem is that the Arduino does not respond to my input.

AWOL

Quote
There are no debug problems that I can see.
Good, so share it.

And the swallowing of characters (whether or not there's one to read) and doing nothing with it - that's working for you, is it?

AWOL

Code: [Select]
Serial.println("Shutting down...");
    delay(3);
    noInterrupts();

About enough time to print "Shu" ( assuming you're not already printing something else)

Go Up