polling the serial com port using arduino

how can i continuously check the value in the usb port?

im supposed to have this visual basic program to send "1" to the serial port and i want the arduino to continuously check its value in real time. because if there is a "1" in the port then the arduino is programmed to do something..in the middle of that task of the arduino, and there is another "1" in the port, the arduino will restart what it is doing..

can you help me with the codes? i have tried doing it with a while loop. but all it does is an endless loop of the task..

To trigger a task off a specific character received through the serial port:

if(Serial.available()){
  if(Serial.read() == '1'){
    //do task here
  }
}

If you need that task interrupted and restarted on another value being received from the serial port... that's a bit trickier. You're either going to have to regularly check the serial data throughout the task you're doing, or generate a timed interrupt that checks the serial port at regular intervals, and somehow abort and restart the current task if another '1' is received. It all depends on how instant your real time response needs to be.

If you also need to respond to other serial data coming in through the serial port, that only further complicates things.

Post your code and we'll try to help.

  const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  const int enablePin = 6;    // H-bridge enable pin
  const int override = 5;
 int val = 0;       // variable to store the data from the serial port

void setup() {
 // set the switch as an input:
   pinMode(override, INPUT);

    // set all the other pins as outputs:
    pinMode(motor1Pin, OUTPUT); 
    pinMode(motor2Pin, OUTPUT); 
    pinMode(enablePin, OUTPUT);


    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH); 

  Serial.begin(9600);        // connect to the serial port
}

void loop () {
    
  // send data only when you receive data:
      while(Serial.available() > 0) {
        val= Serial.read();
        Serial.println(val);
        
      if (val == 1) {
        
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      delay(3000);
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay(1000);
      
      while (digitalRead(override) ==HIGH) {
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay(500);
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      delay(3000);
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay(1000);
      }
      
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay(500);
      
      while (digitalRead(override) ==HIGH) {
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay(500);
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      delay(3000);
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay(1000);
      }
      
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay(3000);
      
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      
    } 
    // if the switch is low, motor will turn off
    else {
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, HIGH);   // set leg 2 of the H-bridge high
    }
}
}

This is my code. the override is a switch, which when turned on, should reset the task back to start. this is also what should happen when there is another "1" in the serial port.

i want the arduino to continuously check its value in real time.

     delay(3000);
     delay(1000);
     
     while (digitalRead(override) ==HIGH) {
     delay(500);
     delay(3000);
     delay(1000);
     }
     
     delay(500);
     
     while (digitalRead(override) ==HIGH) {
     delay(500);
     delay(3000);
     delay(1000);
     }
     
     delay(3000);

That's certainly not my definition of real-time.

i would really appreciate your help. thanks! :slight_smile:

You need to get rid of all those delays. This will require a significant restructure of your application. Look at the Blink Without Delay example to get you started.

i'm new here and i really don't know how i can program it for real time checking.so i just put a little delay.

i need a three second delay for each turn of the motor. because i planned to turn a motor clockwise for three seconds and another three seconds for counter clockwise. :slight_smile:

but i'm reading the Blink Without Delay now.

I'm lost now. :frowning:

  const int switchPin = 2;    // switch input
  const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  const int enablePin = 6;    // H-bridge enable pin
  const int override = 5;
  const int pin1=1;
 int val = 0;       // variable to store the data from the serial port
long previousMillis = 0;
void setup() {
 // set the switch as an input:
   pinMode(override, INPUT);
 pinMode(switchPin, INPUT);
    // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT); 
    pinMode(motor2Pin, OUTPUT); 
    pinMode(enablePin, OUTPUT);
    pinMode(pin1, OUTPUT);

    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH); 

  Serial.begin(9600);        // connect to the serial port
}

void loop () {
    
  // send data only when you receive data:
      while(Serial.available() > 0) {
        val= Serial.read();
        Serial.println(val);
        if (val== 1){
        digitalWrite(pin1, HIGH); 
        }
      }
      if (digitalRead(switchPin) == HIGH) {
        unsigned long currentMillis = millis();
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      
      if (digitalRead(override) == HIGH) {
       
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      }
      
 
      if(currentMillis - previousMillis > 3000) {
    // save the last time you blinked the LED 
      previousMillis = currentMillis; 
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      
      if(currentMillis - previousMillis > 1000) {
    // save the last time you blinked the LED 
      previousMillis = currentMillis; 
      
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      
      if(currentMillis - previousMillis > 3000) {
    // save the last time you blinked the LED 
      previousMillis = currentMillis;
      
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      
    } }}
    // if the switch is low, motor will turn off
    else {
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, HIGH);   // set leg 2 of the H-bridge high
    }
}}

I connected motor1Pin to one leg of an LED and motor2Pin to its other leg.
now, the LED only lights up when I switch pin1 to high. It does not do the rest of the code. :cry:

I connected motor1Pin to one leg of an LED and motor2Pin to its other leg.

This doesn't make sense. One leg of the LED should connect to motor1Pin. The other end should connect to ground.

There should be 2 enable pins. The enable pins control direction for one motor each. The motor pins are for two different motors.

i just connected it to two LEDs for checking. i connected their legs alternately to motorpin1 and motorpin2 so that i can vary the motorpins value from low to high or high to low. this will let only one LED to light up when it has one leg connected to a high and it shorter leg connected to a low..

You need to go through your code. Put each { on a separate line. Put each } on a separate line. Indent all the code properly.

The calls to digitalRead() should not be in if statements. You can not verify what value was read when it is not saved.

If the serial input comes from the Serial Monitor, you can use Serial.print() to send data to the Serial Monitor for display.

Separate the code to collect input, from the serial port and the switch pins, from the code to deal with that data.

Add comments before each block to define what that block is supposed to do.

I think that when you do this, it will become obvious what the problem is. If not, post the modified code back here, and we'll have another look at it.