Someone a idea how to run LED in the same time a function is called in the loop?
#include <Servo.h>
#define SDM_IO_TIMEOUT 1000
int IRsensorpin=0;
int servopin = 51;
int sensorpin = 0; // analog pin used to connect the sharp sensor
int dist = 0; // variable to store the values from sensor(initially zero)
const int ledYeright = 24;
const int ledYeleft = 40;
const int ledredright = 30;
const int ledredleft = 36;
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0;
long interval = 500;
boolean running_warningBlink;
int leftdist;
int rightdist;
int groundcontrol;
Servo myservo;
int pos = 0;
// constants won't change. Used here to
// set pin numbers:
void setup() {
// set the digital pin as output:
pinMode(ledYeright,OUTPUT);
pinMode(ledYeleft, OUTPUT);
pinMode(ledredright, OUTPUT);
pinMode(ledredleft, OUTPUT);
myservo.attach(servopin);
}
void loop()
{
look();
delay(5000);
}
void warning_led()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledYeright, ledState);
digitalWrite(ledYeleft, ledState);
digitalWrite(ledredright, ledState);
digitalWrite(ledredleft, ledState);
}
}
void look() {
warning_led();
//void look(int &rightlook, int &leftlook) {
//look left
for(pos = 90; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 40ms for the servo to reach the position
}
// leftdist=getDistance_sensor();
delay(500);
//look right
for(pos=180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 40ms for the servo to reach the position
}
// rightdist=getDistance_sensor();
delay(500);
//look forward
for(pos = 1; pos < 90; pos +=1)
{
myservo.write(pos);
delay(30);
}
}