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);
}