Go Down

Topic: polling the serial com port using arduino (Read 4 times) previous topic - next topic

cess818

i would really appreciate your help. thanks!  :)

PaulS

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.

cess818

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.

cess818

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. :)

but i'm reading the Blink Without Delay now.

cess818

I'm lost now. :(
Code: [Select]
 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.  :'(

Go Up