Philippines
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« on: July 10, 2012, 11:23:27 pm » |
I just got the arduino, and basically, i'm going to make a program that controls a servo independently with a set or instructions, and reads values from a temperature sensor independently and does another action when a certain value is reached.
I'm planning to do this with one arduino, is this possible?
|
|
|
|
|
Logged
|
--Gin319
|
|
|
|
Offline
Edison Member
Karma: 15
Posts: 1001
Arduino rocks
|
 |
« Reply #1 on: July 11, 2012, 12:07:58 am » |
Sure. You just have to make sure your main loop doesn't wait for anything -- essentially, no while loops.
|
|
|
|
|
Logged
|
|
|
|
|
Philippines
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #2 on: July 11, 2012, 03:40:41 am » |
Will the delay command have any effect?
|
|
|
|
|
Logged
|
--Gin319
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19006
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: July 11, 2012, 03:45:26 am » |
It may well do. But we can't see your code.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 29
Posts: 2884
I only know some basic electricity....
|
 |
« Reply #4 on: July 11, 2012, 03:53:36 am » |
You might find this page helpful: http://www.gammon.com.au/forum/?id=11425especially look for the post on state machines.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #5 on: July 11, 2012, 04:48:55 am » |
Will the delay command have any effect? It may well do. I think you misspelled "Most definitely!".
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #6 on: July 11, 2012, 06:18:03 am » |
Will the delay command have any effect?
It will delay execution.
|
|
|
|
|
Logged
|
|
|
|
|
Philippines
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #7 on: July 11, 2012, 11:46:51 pm » |
thanks for your replies. Let's say that I want to control 2 LEDs independently, with different pin connections and effects. WizenedEE mentioned something about the code being in the main loop. How do I make 2 independent loops? I've given values for the pinouts for the second LED, so where do I put it in the code for an independent operation? I got this code from the examples library in the Arduino software, and I added the new pinout on the second line of the code. Regarding the delay, how do I program it without affecting eachother? int led = 13; int led0 = 9;
void setup() { pinMode(led, OUTPUT); pinMode(led0, OUTPUT); }
void loop() { digitalWrite(led, HIGH); delay(1000); digitalWrite(led, LOW); delay(1000); }
|
|
|
|
« Last Edit: July 11, 2012, 11:50:18 pm by Gin319 »
|
Logged
|
--Gin319
|
|
|
|
California
Offline
Edison Member
Karma: 37
Posts: 1828
|
 |
« Reply #8 on: July 12, 2012, 12:00:39 am » |
WizenedEE mentioned something about the code being in the main loop. How do I make 2 independent loops?
You don't. You need to make your code work together within a single loop. Regarding the delay, how do I program it without affecting eachother? Look at the Blink Without Delay example.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #9 on: July 12, 2012, 12:20:01 am » |
Let's say that I want to control 2 LEDs independently, with different pin connections and effects.
http://www.gammon.com.au/blink
|
|
|
|
|
Logged
|
|
|
|
|
Philippines
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #10 on: July 12, 2012, 08:03:35 pm » |
Thanks for the link. I kinda understand how it works. Now let's say that I wanted to take temperature readings and then control a servo motor independently. Using this code that I got from your link, can you please give me an idea on how to place it in this code? Thanks. // Which pins are connected to which LED const byte greenLED = 12; const byte redLED = 13;
// Time periods of blinks in milliseconds (1000 to a second). const unsigned long greenLEDinterval = 500; const unsigned long redLEDinterval = 1000;
// Variable holding the timer value so far. One for each "Timer" unsigned long greenLEDtimer; unsigned long redLEDtimer;
void setup () { pinMode (greenLED, OUTPUT); pinMode (redLED, OUTPUT); greenLEDtimer = millis (); redLEDtimer = millis (); } // end of setup
void toggleGreenLED () { if (digitalRead (greenLED) == LOW) digitalWrite (greenLED, HIGH); else digitalWrite (greenLED, LOW);
// remember when we toggled it greenLEDtimer = millis (); } // end of toggleGreenLED
void toggleRedLED () { if (digitalRead (redLED) == LOW) digitalWrite (redLED, HIGH); else digitalWrite (redLED, LOW);
// remember when we toggled it redLEDtimer = millis (); } // end of toggleRedLED
void loop () {
// Handling the blink of one LED. if ( (millis () - greenLEDtimer) >= greenLEDinterval) toggleGreenLED ();
// The other LED is controlled the same way. Repeat for more LEDs if ( (millis () - redLEDtimer) >= redLEDinterval) toggleRedLED ();
/* Other code that needs to execute goes here. It will be called many thousand times per second because the above code does not wait for the LED blink interval to finish. */
} // end of loop
|
|
|
|
|
Logged
|
--Gin319
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 212
|
 |
« Reply #11 on: July 12, 2012, 08:53:09 pm » |
void setup{ i=0; s=0; r=0;}
void loop { if ( i % 5 == 0 ) { s=1-s; if (s<0){digitalwrite (relight,HIGH); } else {digitalwrite (redlight,LOW)}; // define redlight pin in setup } if ( i % 100 == 0 ) { r=1-r; if (r<0){digitalwrite (greenlight,HIGH); } else {digitalwrite (greenlight,LOW)}; // define greenlight pin in setup } //delay below //.. }
for sure this programs runs to fast to see redlight blink you add delay or use higer numeber after the % .. i % 5000 just another way of doing it, based on modulo calculation
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 37
Posts: 1828
|
 |
« Reply #12 on: July 12, 2012, 09:01:44 pm » |
Using this code that I got from your link, can you please give me an idea on how to place it in this code? BIG Clue here: /* Other code that needs to execute goes here. It will be called many thousand times per second because the above code does not wait for the LED blink interval to finish. */
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #13 on: July 13, 2012, 12:24:49 am » |
Now let's say that I wanted to take temperature readings and then control a servo motor independently. What does this mean. You have one computer and two jobs. How can you expect to do them independently? Doing one right after the other (read the temps; diddle with the servo; read the temps; diddle with the servo) makes sense. Doing the two tasks independently requires two Arduinos. Help me understand what independently means to you.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 29
Posts: 2884
I only know some basic electricity....
|
 |
« Reply #14 on: July 13, 2012, 07:44:37 am » |
Maybe thermometer and servo are the independent parts?
Gin, loop() is your friend. Every time through can be 100 microseconds or less later, if your code doesn't hang things up. Inside loop() you can have a block of code that watches the sensor and the next block of code runs the servo. Each runs then loop() starts again and loop() is perpetually 'now'... but only if your code doesn't block.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
|