I imagine this is a problem that may have been previously solved. If that is the case, i would appreciate if you could post a link to the appropriate reference.
I would like for my script to execute temporally based commands, ie: reading sensor data and acting accordingly.
I have 3 components. An IR Beam, an LED strip, and a servo motor.
If the IR sensor registers a break, i would like the servo to act. However, if the IR Sensor does not register a break in the last 30 seconds, i would like for the LED strip to be illuminated.
My current issue: even though the IR sensor registers a break and the servo is activated, the LED strip still illuminates. I need the strip to not illuminate when the beam is broken: this is where the previous sensor data comes in.
Please see the code.
Thank you in advance!
//LED Strip
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 8
// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.
#define DATA_PIN 9
// Define the array of leds
CRGB leds[NUM_LEDS];
// Brightness- up to 255
uint8_t max_bright = 200;
//IR Beam
#define SENSORPIN 4 //Input for the IR Sensor)
int sensorState = 0;
int lastState = 0; //variable for reading the pushbutton status
//Servo Motor
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 9; // variable to store the servo position
//30 sec Interval after sensor evaluated
unsigned long previousMillis = 0; //storage
long interval = 10000 ; //interval
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
FastLED.setBrightness(max_bright);
Serial.begin(9600); //specific channel for reading the output
void loop() {
//2a. IR beam breaks then door closes
// read the state of the pushbutton value:
sensorState = digitalRead(SENSORPIN);
// check if the sensor beam is broken
// if it is, the sensorState is LOW:
if (sensorState == LOW); {
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.println("Broken"); //if beam not broken after sometime, LED must go off
count++;
}
unsigned long currentMillis = millis();
// turn LED on:
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // turn LED on after 30 seconds
//2b. LED Turns on
while (sensorState == HIGH) { // need to modify to include checking of sensorState within the passed 30 seconds)
leds[0] = CRGB::White;
FastLED.show();
leds[1] = CRGB::White;
FastLED.show();
leds[2] = CRGB::White;
FastLED.show();
leds[3] = CRGB::White;
FastLED.show();
leds[4] = CRGB::White;
FastLED.show();
leds[5] = CRGB::White;
FastLED.show();
leds[6] = CRGB::White;
FastLED.show();
leds[7] = CRGB::White;
FastLED.show();
}
}
}
}
Please read the sticky at the top on how to use the forum = pay special attention about how to post code using the code tags - most people will not go through the several steps to download your code - load it into the IDE then see if they can find the problem then come back here and post a reply - you need to make it easy for someone that only has a few minutes of time to help you
Your code is very hard to read. Also it makes it much easier to help if you include short programs in your Post. I have re-formatted the program to make it easier to read. You can do the same thing with the AutoFormat tool.
/*
IR Breakbeam sensor demo!
*/
//LED Strip
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 8
// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.
#define DATA_PIN 9
// Define the array of leds
CRGB leds[NUM_LEDS];
uint8_t max_bright = 200;
//IR Sensor and Logic
#define SENSORPIN 4
// variables will change:
int sensorState = 0, lastState=0; // variable for reading the pushbutton status
//30 sec Interval after sensor evaluated
unsigned long previousMillis = 0; //storage
long interval = 10000 ; //interval
void setup() {
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
FastLED.setBrightness(max_bright);
// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup
Serial.begin(9600);
}
void loop() {
leds[0] = CRGB::Black;
FastLED.show();
// read the state of the pushbutton value:
sensorState = digitalRead(SENSORPIN);
// check if the sensor beam is broken
// if it is, the sensorState is LOW:
if(sensorState == LOW) {
leds[0] = CRGB::White;
FastLED.show();
}
unsigned long currentMillis = millis();
// turn LED on:
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // turn LED on after 30 seconds
while(sensorState == HIGH) { // need to modify to include checking of sensorState within the passed 30 seconds)
leds[0] = CRGB::White;
FastLED.show();
leds[1] = CRGB::White;
FastLED.show();
leds[2] = CRGB::White;
FastLED.show();
leds[3] = CRGB::White;
FastLED.show();
leds[4] = CRGB::White;
FastLED.show();
leds[5] = CRGB::White;
FastLED.show();
leds[6] = CRGB::White;
FastLED.show();
leds[7] = CRGB::White;
FastLED.show();
}
}
}
Your program does not refer to a servo anywhere so I'm not clear where you want that to go.
The following is a simple way to check if a button is pressed continuously for a certain length of time
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) { // assumes LOW when pressed and HIGH when not-pressed
lastTimeButtonWasHigh = millis();
}
if (millis() - lastTimeButtonWasHigh >= requiredTime) {
// input has been LOW throughout the required time
// do whatever needs to be done
}
}
If you need to ensure that the button has NOT been pressed during that time you will obviously need to invert the logic.
Thank you for the logic. I will try this out, however (and i am still new to coding) it looks like your suggestion would have me defining the millis function twice in my code. Is that workable? As i also need to define it to generate a pause without the delay function.
I have also modified my original post and included the servo code.
alecseidenberg:
would have me defining the millis function twice in my code. Is that workable?
You don't define the millis() function in your code (that is done by the Arduino system). You USE the millis() function in your code and you can do that as often as you want. It can be useful to save the value of millis() to a variable at the start of loop() and then use the value in the variable throughout that iteration of loop() so every time test uses the same value.
As i also need to define it to generate a pause without the delay function.
The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.
I have also modified my original post and included the servo code.
For the future please don't make changes to earlier Posts (other than to correct typos) as it makes the flow of the discussion difficult to follow. For example my comment about the lack of servo code no longer makes sense to someone else reading the Thread. It is also useful to be able to compare an older and a newer version.