I have a project to control my projector via RS-232 commands, and the projector sends feedback to the arduino. Therefore I have a LCD screen with buttons. To make it nicer, I have an ultrasonic rangefinder that I want to use so that when you approach the device, the LCD backlight turns on for 30 seconds, then turns back off. I can't use a delay because I need to continue polling for buttons and serial information from the projector.
There are community libraries like Timer.h and SimpleTimer.h, but these only do oscillations, etc.
What I would like to do is this:
distance = measureUltrasonicDistance(ultrasonicPin); //returns in cm
if (distance <= 10) {
//digitalWrite(baclkightPin,HIGH);
//have this turn off 30 seconds later
}
This seems like a typical application for the event fuse library.
First thing to realize is that loop() is continuously invoked, and that you can use a "watchdog" pattern to keep the light on without using a timer.
unsigned long timeoff;
void setup() {
timeoff = millis();
}
void loop() {
distance = measureUltrasonicDistance(ultrasonicPin); //returns in cm
if (distance <= 10) {
digitalWrite(baclkightPin,HIGH);
// compute boundary of when light should be off
timeoff = millis() + 30L*1000L;
}
if (timeoff < millis()) {
digitalWrite(backlightPin, LOW);
}
}
Hope this helps, let me know if there are any problems.
Related
I am trying to make a security system using a motion sensor in Arduino. I almost have everything working the way I want it to, but I cannot figure out how to make the buzzer go off immediately after 60 seconds have passed while the light is on.
The code should work like this:
When the motion sensor detects someone, the light turns on. if the light is on for 60 seconds, then the buzzer is turned on. The buzzer will not turn off until a button is pressed, which restarts the code.
I can make the buzzer go off if the time is logged (recorded, if you will) anytime above 60 seconds while the light is on, but that doesn't trigger the buzzer until the motion detector doesn't detect anything for 10 straight seconds (which determines when the time is logged)
My code should look something like this, but I'm unsure of the right way to set up a timer:
if (detectedLED, HIGH)
{
Start timer // Start a timer
if ((timer >= 60000)&&(detectedLED, HIGH))
{
digitalWrite(BuzzerPin, HIGH); // Enable Buzzer
BuzzerOnState = true; // Enable Buzzer State
}
}
I think this is what you want.
unsigned long timer = 0;
bool flag = false;
void loop() {
if (detectedLED, HIGH) {
if (!flag) {
flag = true;
timer = millis();
}
if ((millis() - timer) >= 60000) {
digitalWrite(BuzzerPin, HIGH); // Enable Buzzer
BuzzerOnState = true; // Enable Buzzer State
}
} else {
flag = false;
}
}
I'm working with an Arduino for the first time in my life for a school project.
I want to make a coffee reminder alarm, so you don't forget you made a cup of coffee for yourself.
I'm working with an FSR sensor which measures the presence of the coffee cup. When the cup is placed the LED should turn on to show you a timer has started ending with a reminding buzzer.
Right now I'm using a delay, but this isn't working as I want to. Because of the delay you can remove the cup and the timer still goes on with the buzzer at the end.
I want the LED and the timer/buzzer to go off when the cup gets removed midway the process.
I never ever programmed before in my life, and I find it quite hard to get my head around it.
Here's my code:
int fsrPin = 0;
int fsrReading;
int LEDpin = 11;
const int buzzer = 7;
void setup(void) {
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop(void) {
fsrReading = analogRead(0);
Serial.print("Analog reading = ");
Serial.print(fsrReading);
if (fsrReading < 10) {
Serial.println(" - No Cup");
digitalWrite(LEDpin, LOW);
noTone(buzzer);
}
else if (fsrReading > 10) {
Serial.println(" - Cup pressent");
digitalWrite(LEDpin, HIGH);
noTone(buzzer);
delay(10000);
tone(buzzer, 700);
delay(1000);
}
delay(500);
}
The Arduino library has a millis() and a micros() functions that you should be able to make use of. They report the number of milli-seconds or micro-seconds since boot.
You can set a variable to record what time one thing happened, then later see if the the current millis() is more than say 30,000 (30 seconds) after the previous recorded time.
Adafruit has a nice tutorial on this subject, which can be found here.
How to setup Eccp2 With a timer and ZeroCross.
I need to figure out how to open-close an output with comparison the timer and eecp2.
I need to make a motor to work from 25% up to 75%.
I have include Eccp2 and Timer1 and Ext_INT Libraries from mplab. I have set ECCP2 to compare timer1. Also Ext_Int to falling Edge in input INT0.
So the first thing in my main which i'm trying to do is to set interupt everytimer i get zerocross. I dont know if i explain it well...
I mean every time that the input in INT0 is in Ground.
INT0_SetInterruptHandler(ZeroCrossCallback);
Second i found a code which i dont know exactly what its doing...
void ZeroCrossCallback() {
if (!PORTBbits.INT0) {
TMR1_WriteTimer(0);
CCP2CONbits.CCP2M = 9;
EXT_INT0_risingEdgeSet();
} else {
CCP2CONbits.CCP2M = 0;
EXT_INT0_fallingEdgeSet();
}
}
And last I need to set my motor to work from 25% up to 75%
Img
I have been trying for the past few hours to get a light gate timer to work with 2 photocells and lasers. It is like a trip wire, when the 1st sensor is low (when it is tripped), it triggers to start the time. This then is supposed to continue and then stop the timer when the second sensor is triggered using the same method as sensor 1 is triggered. The problem that I am having now is that the timer never stops. It seems to be a problem with my second sensor, I have switched the second sensor and the sensor seems to be fine. The other problem is that the timer does weird things when the second sensor is triggered also. It also seems that each time I reset or place a new version of the code onto the Arduino, it does a different thing, very strange. I have tried various other codes but none seem to work. If you have a similar code or could help me with this code, that would be great.
Below is my code:
int sensor1 = 2;
int sensor2 = 3 ;
long startTime;
long endTime;
float elapsedTime;
long pause = 0;
void setup() {
Serial.begin (9600);
Serial.println ("RPV");
pinMode (sensor1, INPUT);
pinMode (sensor2, INPUT);
}
void loop() {
if (analogRead(sensor1) == 0) {
Start ();
Serial.println (elapsedTime);
}
if (analogRead(sensor2) == 0) {
Stop ();
//Serial.println (endTime);
}
}
void Start(){
startTime = millis();
}
void Stop() {
endTime = millis();
elapsedTime = (endTime - startTime);
elapsedTime = elapsedTime/1000;
Serial.print("Time Seconds: ");
Serial.println(elapsedTime);
}
Try testing the sensors by writing the analogRead to serial.
My guess is that the analogRead will never really be 0 since there will always be ambient light.
Your threshold should be more analogRead(sensor1) < somevalue )
I'm trying to use a mirror galvo controlled by a mega 2560 to direct a laser. As a first test I want the laser to move along one axis by jumping equal intervals, but I'm having trouble. I'd like to know whether there are any obvious problems in my code (which I suspect may be the case as I'm new to timers and interrupts)?
void setup()
{
TCCR3B = _BV(CS30); //high freq ~50khz
pinMode(5, OUTPUT);
}
void loop() {
int output1= 5;
while(output1<255){
analogWrite(5,output1);
output1+=40;
delay(500);
}
output1=5;
}
When I run this code the mirror does achieve the full 5 degree range of movement it should. The problem is instead of moving equal intervals the mirror rotates the equivalent of .8 deg over what I suppose are the first two iterations of the while loop, then the mirror jump around 3.5deg of rotation. Then the mirror goes back to rotating by small increments again .75 deg for the last few iterations of the while loop.
I originally tried using my own settings for all timer and PWM settings, that did not work either:
void setup()
{
TCCR3A = TCCR3B = 0;
TCCR3A = _BV(COM3A1) | _BV(WGM31); //non-inverting
TCCR3B = _BV(CS30) | _BV(WGM32) | _BV(WGM33); //prescalar=1, fast pwm
ICR3= 500;
OCR3A= 6;
pinMode(5, OUTPUT);
}
void loop() {
while(OCR3A<500){
OCR3A+=80;
delay(500);
}
OCR3A=6;
}
Using this second code the mirror rotates by a constant 1 degree and stays put there.
edit: I do have a current gain circuit between the arduino and the mirrors, but I've tested both that circuit and the mirrors with a function generator. Off the function generator they work fine.