How to run multiple processes on Arduino - timer

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.

Related

Arduino Blinking Light not working on Breadboard

I'm just starting my Arduino 30 Days Lost in Space kit and I'm stuck on the blinking LED on the breadboard.
I have some minor experience coding. I suspect that is not the issue, but I've included it below. I have also included a picture of the breadboard. Sorry it's so grainy. Stack doesn't allow for very large files.
I got the light to blink on the HERO Board itself, but when I ran the updated code to get the LED working, BOTH stopped working.
Here is my code:
int LEDPIN = 8;
void setup() {
// put your setup code here, to run once:
pinMode(LEDPIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LEDPIN, HIGH); //High = 5V, LOW = Ground voltage
delay(500);
digitalWrite(LEDPIN, LOW);
delay(500);
}
Help would be much appreciated.

how to know where my program is reading my int

I am learning again after some years I stopped and I have a problem
the code is Supposed to to light a LED with changed time of lighting the LED just lighting non-stop without being off for a sec
int x = 0;
void setup()
{
pinMode(12, OUTPUT);
}
void loop()
{
x = 0;
while (x < 1000) {
digitalWrite(12, HIGH);
delay(1000 - x);
x = x + 100;
}
}
In the Arduino Studio there is an example sketch called "blink".
If you want to blink the built in led, use that.
If you have an external led connected to a pin, you can just replace the LED_BUILTIN macro in that example with your pin number.
Keep in mind that the reason the sketch is using a "macro" is because the pin for the built in led is different for different models of Arduino so the macro is automatically replaced with the correct pin number when you build the sketch for your specific board.
As for your code, the biggest issue is that you never turn off the led.
digitalWrite(12, LOW);
delay(1000 - x);
You need to "write low" to the pin (basically dropping pin voltage to zero) and then wait for some time before turning the led back on by writing high.
I am not sure why you added the while loop, but you probably don't need it.
The loop function already runs in a loop all the time, so if what you want is to blink the led in decreasing intervals, your code should look like this:
int x = 0;
void loop()
{
if (x < 1000) {
digitalWrite(12, HIGH);
delay(1000 - x);
digitalWrite(12, LOW);
delay(1000 - x);
x = x + 100;
} else {
x = 0;
}
}
}
Note that at some point this code will be blinking too fast for you to notice.
You may want to change the condition in if to something smaller like x < 800 so that the minimum interval is never shorter than 1/5 of a second.
byte x; // 100 ms units of blink speed ( 1000ms, 900ms, 800ms, ...100ms )
void loop() {
x = 10;
while (x > 0) {
digitalWrite(12, HIGH);
delay(100*x);
digitalWrite(12, LOW);
delay(100*x);
x--;
}
}
Blinking requires to switch the LED ON and OFF and to wait in both cases :)
BTW: each loop() run takes about 11 sec. That's fine if the arduino isn't supposed to do anything else in parallel.

ArduinoUno R3 Run Continuous routine and update even while in loop of routine

Hi everyone and thanks for looking at my post. I’m trying to get the following to occur and I’m looking for the best method by which to accomplish the task.
I have a sensor input for weight and a LED output that blinks faster as more weight is applied. I have accomplished this using a simple if else loop. See the section of code below.
lpin is my variable for the light output pin.
WeightMeasure() is my function that returns the weight in lbs.
CODE
void setup()
{
pinMode(lpin, OUTPUT);
}
void loop()
{
WeightMeasure();// get the current weight in lbs
Serial.println("The wight of the object is: ");
Serial.print(WeightMeasure);
Serial.println(" lbs");
delay(100);
//runs the if loop to turn on the LED based on weight
if(WeightMeasure > 20){
digitalWrite(lpin, LOW);
delay(50);
Serial.println("I'm more than 20 LBS”);
}
else if(WeightMeasure > 10){
digitalWrite(lpin, HIGH);
delay(800);
digitalWrite(lpin, LOW);
delay(800);
Serial.println("I'm less than 20lbs but more than 10lbs ");
}
else if(WeightMeasure > 5 ){
digitalWrite(lpin, HIGH);
delay(500);
digitalWrite(lpin, LOW);
delay(500);
Serial.println("I'm less than 10lbs but more than 5lbs");
}
else if(WeightMeasure > 3){
digitalWrite(lpin, HIGH);
delay(200);
digitalWrite(lpin, LOW);
delay(200);
Serial.println("I'm less than 5lbs but more than 3lbs");
}
else {
digitalWrite(lpin, HIGH);
Serial.println("I'm less than 3lbs");
}
END CODE
This code is meant to blink more as less weight is applied to the sensor. While this works, it has a major flaw. I have set up the delay for blinks corresponding with each range of weights in each IF ELSE statement. This does not allow for updates in real time. For example, if an item of 20lbs is applied to the sensor, it will delay sampling weight again for 800ms. I would like to achieve the appropriate time length between blinks while the weight is over 20lbs but if the weight drops from 21lbs to 19lbs rapidly, I want it to immediately break out of the 800ms wait delay and begin blinking at the appropriately faster interval. Does anyone have a thought on how best to achieve this? I’m probably doing this completely wrong but I’m using my basic understanding of programming. An example or best method and explanation would be helpful as I’m really just a beginner. If anything is unclear, please ask.
Cheers,
War
Spend some time looking at the BlinkWithoutDelay example code which is included in the Arduino examples. It uses elapsed time to control the LED blinking and doesn't lock out other code from running as delay() does.
Also maybe think about defining a function to do the blink (change the state of the LED pin) rather than repeating the same code in each if/else clause.

Light Gate Timer Arduino

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 )

Start a timer with a function in arduino

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.

Resources