Let's Make Robots!

Loop two things at once

This is our sketch, everything works but we want to make the two functions be able to work at the same time. Anyone who knows how to do that?

 

boolean firstTrigger=false; //tells the duino if the LDR level is below 100

const int sensor=0; //or whatever your LDR sensor is. 

const int MotorPin1 = 3; //or whatever

int buttonPin=11; // pin where the button nr 1 is connected I0

int cupHeater=5;

int buttonState=0;         // variable for reading the pushbutton status

int delayTimeOn=100;

int delayTimeOff=100;

 

void setup(){

  //initialise your motor pins

  pinMode(MotorPin1,OUTPUT);

  Serial.begin(9600);

   buttonState=analogRead(buttonPin);

pinMode(cupHeater,OUTPUT);

pinMode(buttonPin, INPUT);

digitalWrite(buttonPin, HIGH);

}

void loop(){  

  buttonState=digitalRead(buttonPin); // read the state of the pushbutton value

  // check if the pushbutton is pressed.

  // if it is, the buttonState is >512:

  if (buttonState==LOW) {        // turn LED on:  

  digitalWrite(cupHeater,HIGH);

      delay(200000);

      digitalWrite(cupHeater,LOW);

  }

 

  //trigger timer loop by initial activation.

  if (analogRead(sensor) < 100){

    //2 second timer loop. LDR of greater than 100 will break the loop, thus going back to start

    for (int i=0; i < 201; i++){

      if (analogRead(sensor) > 300){ 

        Serial.println("sensor uncovered");

        break;

      }

      if (i == 200){ 

        firstTrigger=true;

        Serial.println("sensor covered 1sec");

      }

      delay(5);//10 x 100 = 1000 ms = 1 seconds

    }

  }

 

  //we've made it at least 2 seconds on low

  if (firstTrigger){

    if (analogRead(sensor) > 400){ 

      Serial.println("starting motor");

      Motor();

    }

  }

 

}//end of primary loop

 

void Motor(){

  //assumes an H-bridge of some sort

  firstTrigger=false;

  analogWrite(MotorPin1, 255);         

  delay(1000);

  for(int fadeValue = 255 ; fadeValue >= 180; fadeValue -=10) { 

    // sets the value (range from 0 to 255):

    analogWrite(MotorPin1, fadeValue);         

    // wait for 30 milliseconds to see the dimming effect    

    delay(50);                            

 }  

 

  digitalWrite(MotorPin1, LOW);

 

}

 

 

 

 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Before calculon pastes this, let him ask you to please comment your code, and if you're copying code from another source, change the comments so they match your code. Or just delete them. If they don't apply to your code, it's incredibly misleading to the people who are trying to understand it.

boolean firstTrigger=false; //tells the duino if the LDR level is below 100

const int sensor=0; //or whatever your LDR sensor is. 

const int MotorPin1 = 3; //or whatever

int buttonPin=11; // pin where the button nr 1 is connected I0

int cupHeater=5;

int buttonState=0;         // variable for reading the pushbutton status

int delayTimeOn=100;

int delayTimeOff=100;

int heaterCount=0; //main-loop timer for heater pause

 

 

void setup(){

    Serial.begin(9600);

    //initialise your motor pins

    pinMode(MotorPin1,OUTPUT);

//   buttonState=analogRead(buttonPin);  //Q? Why do you have this

   pinMode(cupHeater,OUTPUT);

   pinMode(buttonPin, INPUT);

  digitalWrite(buttonPin, HIGH);  // activate internal pullup resitor?

}

 

void loop(){  

   //HEATER STAGE: if the button has been pushed, plate will be heated while counter > 0

  if (heaterCount > 0){

      digitalWrite(cupHeater,HIGH);

      delay(5);

      heaterCount--;

  }

  else{ 

     digitalWrite(cupHeater,LOW);

  } 

 

 //test for button push 

  buttonState=digitalRead(buttonPin); // read the state of the pushbutton value

  if (buttonState==LOW) { 

    heaterCount=20000;

  }

 

  //Peek-aboo stage

  //trigger timer loop by initial activation.

  if (analogRead(sensor) < 100){

   //2 second timer loop. LDR of greater than 100 will break the loop, thus going back to start

    for (int i=0; i < 201; i++){

      if (analogRead(sensor) > 300){ 

        Serial.println("sensor uncovered");

        break;

      }

 

      if (i == 200){ 

        firstTrigger=true;

        Serial.println("sensor covered 1sec");

      }

      delay(5);//10 x 100 = 1000 ms = 1 seconds

    }

  }

 

  //we've made it at least 2 seconds on low

  if (firstTrigger){

    if (analogRead(sensor) > 400){ 

      Serial.println("starting motor");

      Motor();

    }

  }

}//end of primary loop

 

void Motor(){

  firstTrigger=false;

  analogWrite(MotorPin1, 255);         

  delay(1000);

  for(int fadeValue = 255 ; fadeValue >= 180; fadeValue -=10) { 

    // sets the value (range from 0 to 255):

    analogWrite(MotorPin1, fadeValue);         

    // wait for 30 milliseconds to see the dimming effect    

    delay(50);                            

 }  

  digitalWrite(MotorPin1, LOW);

 

}

You became my favorite person of the week! :D

Thank you so much for your help. 

And I promise to take better care of the comments in the future!!! 

 

:* 

Thanks

Maria and Moon (the robot)

Thanks for your answer. 

 

One of my buttons turnes on a heating plate for 200 sec. 

But at the same time I want to "push a button" to activate a motor. 

It seems like the website only shows how to make one thing continue constantly but I just need them both to work while these 200 sec's are ticking.  

 

 

Did you look at the ’Blink without delay’ example? And if so, -did you understand it?
Very few microcontrollers can handle parallel task (with the exception of the Propeller).  So try making your program around the blink without delay. The trick is to use the internal counter millis() that runs in the background of all Arduino sketches. But instead of blocking your microcontroller for 200000ms you need to store the timer value when you want the heat on, then for every loop in your main loop you check if the current millis() is bigger than your stored value + 200000, -then the 200000ms have elapsed.

I’m not sure that I understand how this is supposed to work. But you might look into using a function like in the ‘Blink Without Delay’ http://arduino.cc/en/Tutorial/BlinkWithoutDelay as the delay(200000) is a blocking command and the Arduino isn’t doing anyting until the delay is complete.
Also you might put your button function in an interrupt function so that it’s executed when the button is pressed and you don’t have to do this constant polling.
http://arduino.cc/en/Reference/AttachInterrupt