0
Offline
Newbie
Karma: 0
Posts: 15
^_^
|
 |
« on: January 20, 2011, 04:40:15 pm » |
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
God Member
Karma: 13
Posts: 776
There are 10 kinds of people, those who know binary, and those who don't.
|
 |
« Reply #1 on: January 20, 2011, 04:51:40 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #2 on: January 20, 2011, 04:51:50 pm » |
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.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
^_^
|
 |
« Reply #3 on: January 21, 2011, 05:35:13 pm » |
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
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #4 on: January 21, 2011, 05:37:57 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
^_^
|
 |
« Reply #5 on: January 21, 2011, 05:38:54 pm » |
i would really appreciate your help. thanks! 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #6 on: January 21, 2011, 05:41:58 pm » |
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
Newbie
Karma: 0
Posts: 15
^_^
|
 |
« Reply #7 on: January 21, 2011, 05:42:38 pm » |
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
Newbie
Karma: 0
Posts: 15
^_^
|
 |
« Reply #8 on: January 21, 2011, 05:46:08 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
^_^
|
 |
« Reply #9 on: January 21, 2011, 07:44:20 pm » |
I'm lost now.  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
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #10 on: January 21, 2011, 08:12:34 pm » |
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
Newbie
Karma: 0
Posts: 15
^_^
|
 |
« Reply #11 on: January 22, 2011, 11:01:24 pm » |
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
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #12 on: January 23, 2011, 07:41:19 am » |
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
|
|
|
|
|
|