Hi, so my code was working fine taking reading every second but as soon as i grouped different sketches together the sensor no longer wanted to work properly. i have a traffic light system and the sensor is only taking a reading per cycle of the lights. Ive been looking at the code a while now but cant find the issue. Any help would be much appreciated. Here is the code:
//Lights
int redPin[2] = {10, 7};
int greenPin[2] = {9, 6};
int bluePin[2] = {8, 5};
//light
void setColor(int led, int red, int green, int blue)
{
analogWrite(redPin[led], red);
analogWrite(greenPin[led], green);
analogWrite(bluePin[led], blue);
}
//motor
#include <Servo.h>
Servo myservo;
//sensor
#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;
void setup()
{
//start of lights
for (int led = 0; led < 2; led++) {
pinMode(redPin[led], OUTPUT);
pinMode(greenPin[led], OUTPUT);
pinMode(bluePin[led], OUTPUT);
}
//end of lights
Serial.begin(9600);
myservo.attach(3);
myservo.write(180);
}
void loop()
{
{
setColor(1, 255, 0, 0); // red
setColor(0, 0, 255, 0); // green
delay(2000);
setColor(1, 128, 128, 0); // green
setColor(0, 255, 0, 0); // red
delay(2000);
setColor(1, 0, 0, 0);
setColor(0, 0, 0, 0);
delay(2);
setColor(1, 255, 255, 255);
setColor(0, 255, 255, 255);
delay(2000);
}
//
a=sr04.Distance();
Serial.print(a);
Serial.println("cm");
//
if (a <10) {
myservo.write(90);
delay(200);
myservo.write(200);
delay(200);
myservo.write(90);
delay(200);
}}
////