Hey, I have this code but been working on options to make it non-blocking for the second part of the if, and else. The code works but not properly, any advice?
#include <Servo.h>
//This first part is where the LDR variables are defined
const int ldrPin1 = A1; // we define were the LDR is connected in the Arduino Board
const int ldrPin2 = A2;
const int ldrPin3 = A3;
//This second part is where the pressure variables are defined
int AnalogPin = 0; // This is the pressure sensor in the analog pin 0
int LEDpin = 6; // This is where the LEDs of the fade are connected, in pin 6
Servo servo;
int reading;
int pression;
int fadeValue = 0;
unsigned long lastT;
bool anada = false;
bool timerOn = false;
//In the void setup, we declare the variables for both sensors
void setup() {
Serial.begin(9600); //We set a quote to see the status in the serial monitor
pinMode(LEDpin, OUTPUT); //We will define the LEDpin as an output
servo.attach(9); //The servo is attached in the pin 9
servo.write(0); //The servo starts at this value
lastT = millis();
}
void loop() {
//In this part we will define the Part of the LDR SENSOR LOOP
int ldrStatus1 = analogRead(ldrPin1);
int ldrStatus2 = analogRead(ldrPin2);
int ldrStatus3 = analogRead(ldrPin3);
int LEDStatus = analogRead(LEDpin);
int servoStatus = analogRead(servo.attach(9));
if (ldrStatus1 <= 400 || ldrStatus2 <= 400 || ldrStatus3 <= 400) {
if (timerOn == false) {
lastT = millis();
timerOn = true;
anada = true;
}
if (millis() - lastT < 10000) {
if (anada == true) {
analogWrite(LEDpin, fadeValue);
fadeValue += 2;
if (fadeValue >= 255) {
anada = false;
}
} else {
analogWrite(LEDpin, fadeValue);
fadeValue -= 2;
if (fadeValue < 0) {
anada = true;
}
}
} else {
timerOn = false;
}
} else {
analogWrite(LEDpin, 0);
}
if (analogRead(A0) <= 20)
{
servo.write(0);
}
else {
// servo turns from 0 to 180, waits one second
servo.write(180);
delay(1000);
// servo turns from 180 to 90, waits one second
servo.write(90);
delay(1000);
// servo turns from 90 to 0, waits one second
servo.write(0);
delay(1000);
}
}
#include <Servo.h>
//This first part is where the LDR variables are defined
const int ldrPin1 = A1; // we define were the LDR is connected in the Arduino Board
const int ldrPin2 = A2;
const int ldrPin3 = A3;
//This second part is where the pressure variables are defined
int AnalogPin = 0; // This is the pressure sensor in the analog pin 0
int LEDpin = 6; // This is where the LEDs of the fade are connected, in pin 6
Servo servo;
int reading;
int pression;
int fadeValue = 0;
unsigned long lastT;
bool anada = false;
bool timerOn = false;
//In the void setup, we declare the variables for both sensors
void setup() {
Serial.begin(9600); //We set a quote to see the status in the serial monitor
pinMode(LEDpin, OUTPUT); //We will define the LEDpin as an output
servo.attach(9); //The servo is attached in the pin 9
servo.write(0); //The servo starts at this value
lastT = millis();
}
void loop() {
//In this part we will define the Part of the LDR SENSOR LOOP
int ldrStatus1 = analogRead(ldrPin1);
int ldrStatus2 = analogRead(ldrPin2);
int ldrStatus3 = analogRead(ldrPin3);
int LEDStatus = analogRead(LEDpin);
int servoStatus = analogRead(servo.attach(9));
if (ldrStatus1 <= 400 || ldrStatus2 <= 400 || ldrStatus3 <= 400) {
if (timerOn == false) {
lastT = millis();
timerOn = true;
anada = true;
}
if (millis() - lastT < 10000) {
if (anada == true) {
analogWrite(LEDpin, fadeValue);
fadeValue += 2;
if (fadeValue >= 255) {
anada = false;
}
} else {
analogWrite(LEDpin, fadeValue);
fadeValue -= 2;
if (fadeValue < 0) {
anada = true;
}
}
} else {
timerOn = false;
}
} else {
analogWrite(LEDpin, 0);
}
if (analogRead(A0) <= 20)
{
servo.write(0);
}
else {
// servo turns from 0 to 180, waits one second
servo.write(180);
delay(1000);
// servo turns from 180 to 90, waits one second
servo.write(90);
delay(1000);
// servo turns from 90 to 0, waits one second
servo.write(0);
delay(1000);
}
}
#include <Servo.h>
//This first part is where the LDR variables are defined
const int ldrPin1 = A1; // we define were the LDR is connected in the Arduino Board
const int ldrPin2 = A2;
const int ldrPin3 = A3;
//This second part is where the pressure variables are defined
int AnalogPin = 0; // This is the pressure sensor in the analog pin 0
int LEDpin = 6; // This is where the LEDs of the fade are connected, in pin 6
Servo servo;
int reading;
int pression;
int fadeValue = 0;
unsigned long lastT;
bool anada = false;
bool timerOn = false;
//In the void setup, we declare the variables for both sensors
void setup() {
Serial.begin(9600); //We set a quote to see the status in the serial monitor
pinMode(LEDpin, OUTPUT); //We will define the LEDpin as an output
servo.attach(9); //The servo is attached in the pin 9
servo.write(0); //The servo starts at this value
lastT = millis();
}
unsigned long servoDelayTime = 1000;
unsigned long servoPastTime = millis();
int selectCounter = 0;
bool DoServoThing = false;
bool TickTock = True;
void loop() {
//In this part we will define the Part of the LDR SENSOR LOOP
int ldrStatus1 = analogRead(ldrPin1);
int ldrStatus2 = analogRead(ldrPin2);
int ldrStatus3 = analogRead(ldrPin3);
int LEDStatus = analogRead(LEDpin);
int servoStatus = analogRead(servo.attach(9));
if (ldrStatus1 <= 400 || ldrStatus2 <= 400 || ldrStatus3 <= 400)
{
if (timerOn == false) {
lastT = millis();
timerOn = true;
anada = true;
}
if (millis() - lastT < 10000) {
if (anada == true) {
analogWrite(LEDpin, fadeValue);
fadeValue += 2;
if (fadeValue >= 255) {
anada = false;
}
} else {
analogWrite(LEDpin, fadeValue);
fadeValue -= 2;
if (fadeValue < 0) {
anada = true;
}
}
} else {
timerOn = false;
}
} else {
analogWrite(LEDpin, 0);
}
if ( (analogRead(A0) <= 20) && (DoServoThing == false) )
{
servo.write(0);
} else {
if ( TickTock )
{
TickTock = false;
DoServoThing = true;
servoPastTime = millis();
selectCounter = 0;
}
}
if ( DoServoThing )
{
switch ( selectCounter )
{
case 0:
// servo turns from 0 to 180, waits one second
servo.write(180);
if ( (millis() - servoPastTime) >= servoDelayTime )
{
selectCounter = 1;
servoPastTime = millis();
}
break;
case 1:
servo.write(90);
if ( (millis() - servoPastTime) >= servoDelayTime )
{
selectCounter = 2;
servoPastTime = millis();
}
break;// servo turns from 90 to 0, waits one second
case 2:
servo.write(0);
if ( (millis() - servoPastTime) >= servoDelayTime )
{
selectCounter = 0;
servoPastTime = millis();
DoServoThing = false;
TickTock = true;
}
default:
DoServoThing = false;
selectCounter = 0;
TickTock = true;
break;
}
}
}
You may need to troubleshoot or adjust to suit your requirements.
#include <Servo.h>
//This first part is where the LDR variables are defined
const int ldrPin1 = A1; // we define were the LDR is connected in the Arduino Board
const int ldrPin2 = A2;
const int ldrPin3 = A3;
//This second part is where the pressure variables are defined
int AnalogPin = 0; // This is the pressure sensor in the analog pin 0
int LEDpin = 6; // This is where the LEDs of the fade are connected, in pin 6
Servo servo;
int fadeValue = 0;
int fadeDir = 2;
char s [80];
// -----------------------------------------------------------------------------
unsigned long msecLst;
byte servoPos [] = { 180, 90, 0 };
byte servoIdx;
void
doServo (void)
{
int anlg = analogRead (A0);
if (anlg < 20) {
unsigned long msec = millis ();
if ( (msec - msecLst) > 1000) {
msecLst = msec;
sprintf (s, "%s: servo pos %d", __func__, servoPos [servoIdx]);
Serial.println (s);
servo.write (servoPos [servoIdx]);
servoIdx++;
if (sizeof(servoPos) <= servoIdx)
servoIdx = 0;
}
}
else
servo.write(0);
}
// -----------------------------------------------------------------------------
void loop() {
//In this part we will define the Part of the LDR SENSOR LOOP
int ldrStatus1 = analogRead(ldrPin1);
int ldrStatus2 = analogRead(ldrPin2);
int ldrStatus3 = analogRead(ldrPin3);
// int LEDStatus = analogRead(LEDpin);
if (ldrStatus1 <= 400 || ldrStatus2 <= 400 || ldrStatus3 <= 400) {
analogWrite(LEDpin, fadeValue);
fadeValue += fadeDir;
if (0 >= fadeValue || 255 <= fadeValue)
fadeDir = -fadeDir;
Serial.println (fadeValue);
}
else
analogWrite(LEDpin, 0);
doServo ();
}
// -----------------------------------------------------------------------------
//In the void setup, we declare the variables for both sensors
void setup() {
Serial.begin(9600); //We set a quote to see the status in the serial monitor
pinMode(LEDpin, OUTPUT); //We will define the LEDpin as an output
servo.attach(9); //The servo is attached in the pin 9
servo.write(0); //The servo starts at this value
}
You can't do an analogRead() from a digital pin. Not even a PWM output pin. I think you may mean int LEDStatus = digitalRead(LEDpin);
You already attached Pin 9 to the servo.
The value returned by servo.attach() is not an analog input pin.
I don't know what you mean to put here. Maybe read back the last position sent to the servo? That's int servoStatus = servo.read();