Go Down

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

goodinventor

Yes, I did. Thank you for reminding me.

goodinventor

Are there any more improvements I can make?

AWOL


goodinventor

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

AWOL

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.

goodinventor

OK. Thank you for the information. I will try out your suggestion in the future.

goodinventor

OK. I implemented millis() in the LED command. I think I'll implement millis() instead of delay() for everything. And I might add a timer function.

goodinventor

Fellow hackers, I made the changes to the program. The compiler generated errors when I tried to compile the program.

Here is the program so far.

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
long LED_BLINK_INTERVAL = 500; // time interval for LED to blink
unsigned long TIME_NEEDED

int Time(TIME_NEEDED)
{
  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 setup()
{
  Serial.begin(9600);
 
  pinMode(LED_PIN, OUTPUT); // LED pin
 
  Serial.println("Welcome...");
  TIME_NEEDED = 3000;
  Time(TIME_NEEDED);
 
  if (Time(TIME_NEEDED) == 1)
  {
    Serial.println("root@unos:~#");
  }
 
  TIME_NEEDED = 3000;
  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();
 
  switch (inputChar)
  {
    case 's':
      input[0] = inputChar;     
      Serial.println(inputChar);
     
      Serial.println("Shutting down...");
      delay(3000);
     
      noInterrupts();
      break;
     
    case 'r':
      input[0] = inputChar;
      Serial.println(inputChar);
     
      Serial.println("Restarting...");
      delay(3000);
     
      noInterrupts();
     
      interrupts();
     
      Serial.println("Welcome...");
      delay(3000);
     
      Serial.println("root@unos:~#");
      delay(3000);
 
      Serial.print("root@unos:");
      break;
   
    case 'p':
      input[0] = inputChar;
      Serial.println(inputChar);
      Serial.println(inputChar);
     
      Serial.println("root@unos:~#");
      delay(3000);
 
      Serial.print("root@unos:");
      break;
     
    case 'b':
      input[0] = inputChar;
      Serial.println(inputChar);
     
      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(3000);
 
      Serial.print("root@unos:");
      break;
  }
}


Here are the errors that the compiler generated.

Unos_0_0_1:12: error: expected initializer before 'int'
Unos_0_0_1:15: error: expected initializer before 'int'

What does this mean? And how can I fix it?

AWOL

#23
Jan 01, 2015, 01:11 am Last Edit: Jan 01, 2015, 01:12 am by AWOL
Needs more semicolon.
And type specifier.

KenF

TIME_NEEDED is a global variable.  So there's no need to pass it to your function Time. 


BTW You've missed a semi-colon on line 14;

goodinventor

OK, now I have the following errors.

Unos_0_0_1:10: error: 'TIME_NEEDED' was not declared in this scope
Unos_0_0_1:15: error: redefinition of 'int Time'
Unos_0_0_1:10: error: 'int Time' previously defined here

Here is my 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;

int Time(TIME_NEEDED)
{
  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 setup()
{
  Serial.begin(9600);
 
  pinMode(LED_PIN, OUTPUT); // LED pin
 
  Serial.println("Welcome...");
  TIME_NEEDED = 3000;
  Wait(TIME_NEEDED);
 
  if (Time(TIME_NEEDED) == 1)
  {
    Serial.println("root@unos:~#");
  }
 
  TIME_NEEDED = 3000;
  Time(TIME_NEEDED);
 
  if (Wait(TIME_NEEDED) == 1)
  {
    Serial.print("root@unos:");
  }
 
  interrupts();
}

void loop()
{
  // input character
  char inputChar = Serial.read();
 
  char input[8];
 
  unsigned long time = millis();
 
  switch (inputChar)
  {
    case 's':
      input[0] = inputChar;     
      Serial.println(inputChar);
     
      Serial.println("Shutting down...");
      delay(3000);
     
      noInterrupts();
      break;
     
    case 'r':
      input[0] = inputChar;
      Serial.println(inputChar);
     
      Serial.println("Restarting...");
      delay(3000);
     
      noInterrupts();
     
      interrupts();
     
      Serial.println("Welcome...");
      delay(3000);
     
      Serial.println("root@unos:~#");
      delay(3000);
 
      Serial.print("root@unos:");
      break;
   
    case 'p':
      input[0] = inputChar;
      Serial.println(inputChar);
      Serial.println(inputChar);
     
      Serial.println("root@unos:~#");
      delay(3000);
 
      Serial.print("root@unos:");
      break;
     
    case 'b':
      input[0] = inputChar;
      Serial.println(inputChar);
     
      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(3000);
 
      Serial.print("root@unos:");
      break;
  }
}


How can I solve these errors?

KenF

TIME_NEEDED is a global variable.  So there's no need to pass it to your function Time. 
So why do I still find a global variable with the name TIME_NEEDED and a function still having it passed.
Code: [Select]

unsigned long TIME_NEEDED;  //<<<<  This is a global variable, it doesn't need to be passed to any function

int Time(TIME_NEEDED)  //<< This is declaring a function that is going to have TIME_NEEDED passed to it
                                 //(You've also forgotten to declare what type of variable TIME_NEEDED is here)

goodinventor

OK. Here is my improved version. I have fixed all the errors and tested it.

Code: [Select]


[/c//
// 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;

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 setup()
{
  Serial.begin(9600);
 
  pinMode(LED_PIN, OUTPUT); // LED pin
 
  Serial.println("Welcome...");
  TIME_NEEDED = 3000;
  Time(TIME_NEEDED);
 
  if (Time(TIME_NEEDED) == 1)
  {
    Serial.println("root@unos:~#");
  }
 
  TIME_NEEDED = 3000;
  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();
 
  switch (inputChar)
  {
    case 's':
      input[0] = inputChar;     
      Serial.println(inputChar);
     
      Serial.println("Shutting down...");
      delay(3000);
     
      noInterrupts();
      break;
     
    case 'r':
      input[0] = inputChar;
      Serial.println(inputChar);
     
      Serial.println("Restarting...");
      TIME_NEEDED = 3000;
      Time(TIME_NEEDED);
     
      if (Time(TIME_NEEDED) == 1)
      {
        noInterrupts();
      }
     
      interrupts();
     
      Serial.println("Welcome...");
      TIME_NEEDED = 3000;
      Time(TIME_NEEDED);
     
      if (Time(TIME_NEEDED) == 1)
      {
        Serial.println("root@unos:~#");
      }
     
      TIME_NEEDED = 3000;
      Time(TIME_NEEDED);
     
      if (Time(TIME_NEEDED) == 1)
      {
        Serial.print("root@unos:");
      }
     
      break;
   
    case 'p':
      input[0] = inputChar;
      Serial.println(inputChar);
      Serial.println(inputChar);
     
      Serial.println("root@unos:~#");
      TIME_NEEDED = 3000;
      Time(TIME_NEEDED);
     
      if (Time(TIME_NEEDED) == 1)
      {
        Serial.print("root@unos:");
      }
     
      break;
     
    case 'b':
      input[0] = inputChar;
      Serial.println(inputChar);
     
      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 = 3000;
      Time(TIME_NEEDED);
     
      if (Time(TIME_NEEDED) == 1)
      {
        Serial.print("root@unos:");
      }
     
      break;
  }
}


What should I add to the terminal?

KenF

Just for the sake of getting my point across, does it still work like this?

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;

int Time()
{
  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 setup()
{
  Serial.begin(9600);
 
  pinMode(LED_PIN, OUTPUT); // LED pin
 
  Serial.println("Welcome...");
  TIME_NEEDED = 3000;
  Time();
 
  if (Time() == 1)
  {
    Serial.println("root@unos:~#");
  }
 
  TIME_NEEDED = 3000;
  Time();
 
  if (Time() == 1)
  {
    Serial.print("root@unos:");
  }
 
  interrupts();
}

void loop()
{
  // input character
  char inputChar = Serial.read();
 
  char input[8];
 
  unsigned long time = millis();
 
  switch (inputChar)
  {
    case 's':
      input[0] = inputChar;     
      Serial.println(inputChar);
     
      Serial.println("Shutting down...");
      delay(3000);
     
      noInterrupts();
      break;
     
    case 'r':
      input[0] = inputChar;
      Serial.println(inputChar);
     
      Serial.println("Restarting...");
      TIME_NEEDED = 3000;
      Time();
     
      if (Time() == 1)
      {
        noInterrupts();
      }
     
      interrupts();
     
      Serial.println("Welcome...");
      TIME_NEEDED = 3000;
      Time();
     
      if (Time() == 1)
      {
        Serial.println("root@unos:~#");
      }
     
      TIME_NEEDED = 3000;
      Time();
     
      if (Time() == 1)
      {
        Serial.print("root@unos:");
      }
     
      break;
   
    case 'p':
      input[0] = inputChar;
      Serial.println(inputChar);
      Serial.println(inputChar);
     
      Serial.println("root@unos:~#");
      TIME_NEEDED = 3000;
      Time();
     
      if (Time() == 1)
      {
        Serial.print("root@unos:");
      }
     
      break;
     
    case 'b':
      input[0] = inputChar;
      Serial.println(inputChar);
     
      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 = 3000;
      Time();
     
      if (Time() == 1)
      {
        Serial.print("root@unos:");
      }
     
      break;
  }
}


goodinventor


Go Up