Pages: [1]   Go Down
Author Topic: polling the serial com port using arduino  (Read 3239 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
^_^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 779
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24344
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post your code and we'll try to help.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
^_^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 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.
« Last Edit: January 21, 2011, 05:37:04 pm by vmarie_09 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i want the arduino to continuously check its value in real time.
Code:
    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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
^_^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i would really appreciate your help. thanks!  smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
^_^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
^_^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley

but i'm reading the Blink Without Delay now.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
^_^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm lost now. smiley-sad
Code:
 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.  :'(
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
^_^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: