Serial port control problem

i am doing some programming with my arduino to help me control a Microwave frequency converter i have built - i am trying to make a little control box that uses a Nano to drive 4 TTL control lines from different commands sent via the serial port to… pretty simple stuff…

each state is entered by typing 1/ or 2/… /6 this sets the four TTL lines to a cetain state.

I have 6 states i want the control lines to be in. The 6th state i want to have one of the lines turning on and off which i have sucessfully done.

What i can’t seem to do is once the code has entered the 6th state function i can’t write a new serial command and change the TTL lines back to one of the other 5 states

here is the code, the “if (digitalState == 6)…” is the function i am having trouble with with…

Thanks in advance to those who want to help out a Microwave engineer :wink:

Dan.

/*  4 Line LED drive via serila command

Type
1/  for 0101
2/  for 0110
3/  for 1000
4/  for 1001
5/  for 1010
6/  For what ever was programmed befor but with pin 4 blinking

*/

unsigned long serialdata;
int inbyte;
int digitalState;
int pinNumber;


void setup()
{
  Serial.begin(9600);
}

void loop()
{
  getSerial();
 
                  digitalState = serialdata;
                  pinMode(2, OUTPUT); // sets pin to output
                  pinMode(3, OUTPUT); // sets pin to output
                  pinMode(4, OUTPUT); // sets pin to output
                  pinMode(5, OUTPUT); // sets pin to output
                  //Serial.println(serialdata);
        
                if (digitalState == 1)
                {
                  Serial.print("18-23GHz Selected ");
                  digitalWrite(2, HIGH); // SW1_1
                  digitalWrite(3, LOW);  // SW1_1
                  digitalWrite(4, HIGH); // SW1_1
                  digitalWrite(5, LOW);  // SW1_1
                }
              
                 if (digitalState == 2)
                {
                  Serial.print("23-26.5GHz Selected ");
                  digitalWrite(2, LOW);
                  digitalWrite(3, HIGH);
                  digitalWrite(4, HIGH);
                  digitalWrite(5, LOW);
                }
                if (digitalState == 3)
                {
                  Serial.print("26.5-33.599GHz Selected ");
                  digitalWrite(2, LOW);
                  digitalWrite(3, LOW);
                  digitalWrite(4, LOW);
                  digitalWrite(5, HIGH);
                }
                if (digitalState == 4)
                {
                  Serial.print("33.6-37.99GHz Selected ");
                  digitalWrite(2, HIGH);
                  digitalWrite(3, LOW);
                  digitalWrite(46, LOW);
                  digitalWrite(5, HIGH);
                }
                if (digitalState == 5)
                {
                  Serial.print("38-40GHz Selected ");
                  digitalWrite(2, LOW);
                  digitalWrite(3, HIGH);
                  digitalWrite(4, LOW);
                  digitalWrite(5, HIGH);
                }
                    
                if (digitalState == 6)  // makes pin 4 blink on and off - How do you allow this function to exit due to new serial command????
                {
                  Serial.print("Pulse ");
                 while (digitalState == 6)
                {
                //  cal();
                 digitalWrite(4, HIGH);
                  delay(100);
                  digitalWrite(4, LOW);
                  delay(200);
                //  serialdata = Serial.read();
                  Serial.println(serialdata);   
               // How do i make this loop exit when i type an other serial command   ????
              }
                 
                }
 }


long getSerial()    // Get serial data function
{
  serialdata = 0;
  while (inbyte != '/')
  {
    inbyte = Serial.read(); 
    if (inbyte > 0 && inbyte != '/')
    {
      serialdata = serialdata * 10 + inbyte - '0';
    
      Serial.print("   inbyte = ");
      Serial.println(inbyte);
    }
  }
 
  inbyte = 0;
   Serial.print("serial read function serialdata = ");
   Serial.println(serialdata);
  return serialdata;
  }

/[code]

[/code]

Hello,

Something like this?

/*  4 Line LED drive via serila command

Type
1  for 0101
2  for 0110
3  for 1000
4  for 1001
5  for 1010
6  For what ever was programmed befor but with pin 4 blinking

*/

unsigned long serialdata;
int inbyte;
int digitalState;
int pinNumber;


void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT); // sets pin to output
  pinMode(3, OUTPUT); // sets pin to output
  pinMode(4, OUTPUT); // sets pin to output
  pinMode(5, OUTPUT); // sets pin to output
  //Serial.println(serialdata);      
}

void loop()
{
                  digitalState = Serial.read(); //implement the check for ascii digits on this. 
                  
                if (digitalState == '1')
                {
                  Serial.print("18-23GHz Selected ");
                  digitalWrite(2, HIGH); // SW1_1
                  digitalWrite(3, LOW);  // SW1_1
                  digitalWrite(4, HIGH); // SW1_1
                  digitalWrite(5, LOW);  // SW1_1
                }
              
                 if (digitalState == '2')
                {
                  Serial.print("23-26.5GHz Selected ");
                  digitalWrite(2, LOW);
                  digitalWrite(3, HIGH);
                  digitalWrite(4, HIGH);
                  digitalWrite(5, LOW);
                }
                if (digitalState == '3')
                {
                  Serial.print("26.5-33.599GHz Selected ");
                  digitalWrite(2, LOW);
                  digitalWrite(3, LOW);
                  digitalWrite(4, LOW);
                  digitalWrite(5, HIGH);
                }
                if (digitalState == '4')
                {
                  Serial.print("33.6-37.99GHz Selected ");
                  digitalWrite(2, HIGH);
                  digitalWrite(3, LOW);
                  digitalWrite(46, LOW);
                  digitalWrite(5, HIGH);
                }
                if (digitalState == '5')
                {
                  Serial.print("38-40GHz Selected ");
                  digitalWrite(2, LOW);
                  digitalWrite(3, HIGH);
                  digitalWrite(4, LOW);
                  digitalWrite(5, HIGH);
                }
                    
                if (digitalState == '6')  // makes pin 4 blink on and off - How do you allow this function to exit due to new serial command????
                {
                  Serial.print("Pulse ");
                 while (digitalState == '6')
                {
                //  cal();
                 digitalWrite(4, HIGH);
                  delay(100);
                  digitalWrite(4, LOW);
                  delay(200);
                  digitalState = Serial.read();
              }
                 
                }
 }

Not sure if you want to keep the LED blinking while receiving more ocmmands or if you want to blink only once.

If you want to turn on and off a blinking function, the easiest would be to integrate the blink without delay example here. :)

Hi,

Thanks for your quick relpy.

i have tried this approach already and found that in your code, function 6 only runs once - the LED blinks once and then exits.

I would like the LED blink until i type in another command. Once my original code goes into the blink state code the ignores any new serial commands.

cheers

Dan

Danp: i have tried this approach already and found that in your code, function 6 only runs once - the LED blinks once and then exits.

Dan-

How about something like

while (digitalState == '6')
{
  //  cal();
  digitalWrite(4, HIGH);
  delay(100);
  digitalWrite(4, LOW);
  delay(200);
  
  if (Serial.available() > 0)
    digitalState = Serial.read();
}

Hope this helps,

Brad KF7FER

EDIT: Perhaps maybe instead of the "digitalState = Serial.read();" line it might be better to do:

if (Serial.available() > 0)
{
  getSerial();
  digitalState = serialdata;
}

But I suspect you get the idea.