Pages: [1]   Go Down
Author Topic: Combining two code  (Read 523 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,

What I want to do is to run simultaneously two codes from one arduino board. I cannot use the blinkwithouthdelay example because my robot has to 'see' where it is going at all times. Anyone has an idea?

Code 1(motor control):
Code:
const int m1r=2;
const int m1l=4;
const int m2f=6;
const int m2b=7;
const int pin=10;
const int pin1=11;
const int threshold = 200;
const int sensorMin = 100;
const int ledPin =  13;


void setup(){
   pinMode(m1r, OUTPUT);
    pinMode(m1l, OUTPUT);
     pinMode(m2f, OUTPUT);
      pinMode(m2b, OUTPUT);
      pinMode(ledPin, OUTPUT);
   digitalWrite(pin,LOW);
   digitalWrite(pin1,HIGH);
}
void loop(){
  int x = 0;
  int analogValue = analogRead(1);
     int analogValue1 = analogRead(0);
  //Motor control   
 if (analogValue > threshold) {
 
   
     digitalWrite(m2b,LOW);
  digitalWrite(m2f,HIGH);
 
   delay(1000);
   
    while (x <7){
     
   digitalWrite(m1r,HIGH);
   digitalWrite(m1l,LOW);
   
   digitalWrite(m2b,LOW);
   digitalWrite(m2f,HIGH);

   
   delay(700);
   
   digitalWrite(m2b,HIGH);
   digitalWrite(m2f,LOW);
   
   digitalWrite(m1r,LOW);
   digitalWrite(m1l,HIGH);
   
   delay(1000);
   
    digitalWrite(m2b,HIGH);
   digitalWrite(m2f,LOW);
   
   digitalWrite(m1r,LOW);
   digitalWrite(m1l,HIGH);
   
   delay(1000);
   x=x+1;
    }
x=0; 
}
   
else{
 
  digitalWrite(m2f,LOW);
  digitalWrite(m2b,HIGH);
digitalWrite(m1r,HIGH);
   digitalWrite(m1l,HIGH);

}
    }
 


Here's the second code:
Code:
int analogValue1 = analogRead(0);
  //Led control   
 if (analogValue1 > threshold1) {
   digitalWrite(Led,HIGH);
 }
 else{
   
   digitalWrite(Led,LOW);
 }



Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 30
Arduino rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Aiko library is your best friend.
http://github.com/geekscape/Aiko

Aiko Events helped me to combine different activity in a very simple way.
Keep only in mind you are not using a preemptive system, so every part of your program must avoid fatal errors!
So Keep them simple!
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,

What I want to do is to run simultaneously two codes from one arduino board. I cannot use the blinkwithouthdelay example because my robot has to 'see' where it is going at all times. Anyone has an idea?
Why not?  What do you think the blinkwithoutdelay is doing?  It's checking every millisecond whether it should do something, and only when a threshold is reached, will it actually react.  So your robot can do the same thing, looking around and scanning it's surroundings every millisecond if you so choose - it's a bit of an overkill if you ask me, but hey, it's your robot.  Unless you're traveling at a high rate of speed, I would think every 100 milliseconds (or ten times a second) should be enough.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well how would I write the code? When I tried placing my main code in the "// here is where you'd put code that needs to be running all the time" part of blinkwithouthdelay, the delays affected the led. An example code would be appreciated.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

BTW Thanks daitangio for the link to aiko. I will try it.
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What delays?  There are no delays in the BlinkWithoutDelay example.  So if there are any delays in your code, then you introduce them.  It's relatively simple if you can wrap your mind around the loop.  It runs constantly, every millisecond.  If you need something to be running constantly, great, you're done.  If you have other things that don't need to be running constantly, then set a delay based on millis() in them.  Consider this code:
Code:
int pause = 250;
void setup() {
  pinMode(13, OUTPUT);
}
void loop() {
  digitalWrite(13, (digitalRead(13) == HIGH ? LOW : HIGH));
  delay(pause);
}
Now, compare it to this:
Code:
int tickMillis = 0;
int pause = 250;
void setup() {
  pinMode(13, OUTPUT);
}
void loop() {
  // I only want this LED to turn on or off every 250 ms (the length of pause)
  if (millis() - tickMillis > pause) {
    digitalWrite(13, (digitalRead(13) == HIGH ? LOW : HIGH));
    tickMillis = millis();
  }
}
They do exactly the same thing, except the first one uses the delay() function whereas the second one doesn't.  But that LED is still only blinking every 250 ms.  Using the first code, you are always waiting for that delay() call to finish before anything else can happen.  With the second one you don't need to because the loop will just continue to run regardless of whether the time has passed or not - it only affects the LED IF that "pause" has been reached.  So anything outside of that if statement will keep running each time the loop cycles.

You can create another if statement that will only run every 5 seconds.  Your LED will still continue to blink every 250ms, regardless.  Put a button check outside of the if statement and you can continuously check its state and react to it, rather than waiting for a cycle (which has a delay() in it) to finish before it doing something.

So you have to rethink your code and remove all your delays() and instead think in millis() and how to use it to simulate a delay().  I just wrote a long post myself the other night of how I created a light display for my daughter that reacts to a button, and has an indicator LED.  You can read the post here.  The button falls outside of anything, so it's always sampled.  The indicator LED has it's own timing, regardless of the delays within each light pattern (which change each time, so even that isn't fixed.)
Logged

Pages: [1]   Go Up
Jump to: