Light Gate Timer Arduino - timer

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 )

Related

how to calculate the speed of an object using an ultrasonic sensor

I am working on a project in which I want to calculate the speed of a toy car using an ultrasonic sensor by measuring the distance and once it reaches the end. We see the time that has passed from when the programme has started. However I have discovered that the sensor I am using is accurate only till 20cm. Any suggestions?
#include <NewPing.h>
int i=0;
float distance;
long duration;
int trigpin = 9;
int echopin = 10;
int s;
boolean warrant=true;
unsigned long time;
void setup() {
// put your setup code here, to run once:
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
// put your main code here, to run repeatedly:
time=millis();
if(warrant==true){
s=distance;
digitalWrite(trigpin, LOW);
delay(2);
digitalWrite(trigpin, HIGH);
delay(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = duration * 0.034 / 2;
if((distance-s)>1){
Serial.println(distance);
}
delay(200);
if(distance>30){
Serial.println(distance/time);
Serial.println("done");
delay(10);
warrant=false;
}}}
Ideally it should measure the speed easily. The code is working perfectly but it breaks down after 20cm. Probably due to the sensor's limitations
If the sensor is accurate only until 20cm and you need to measure bigger, you should have to change the sensor for a better one. The is no code that can make your sensor better.

How to run multiple processes on Arduino

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 loop a section of code under the loop branch only once in Arduino?

I've been struggling with some code lately. Just like I mentioned in my question, I want to loop a single section of code under the loop() block only once. I have done my research and am aware of the two common ways to do this. One is to put the code I want to run once in the setup() block and the second being using the while(1) statement in the loop() block. Unfortunately, both ways are not suitable for my code. The first reason is it is compulsory for my code to be in the loop() section. I cannot use the second option as all the code under the loop() block ends up running once. Like I said before, I want only a section of code in the loop() block to run once.
For your information, the purpose of this code is to display in an LCD, the amount of milliliters left for the user to drink for a healthy water consumption. For example, a person should drink min 1800ml a day for proper hydration. If the user drinks 123ml of water, LCD should display (1800-123).
//Adds the liquid crystal library
#include <LiquidCrystal.h>
//defines lcd pin numbers
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// defines ultra sonic sensor pin numbers
const int trigPin = 8;
const int echoPin = 7;
// defines variables
long duration;
long volume;
long interval = 3600000; //1 hour
unsigned long stime = millis();
double pdist = 0;
double cdist = 0;
double mcons = 4.5; //128ml;
void setup() {
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
Serial.begin(9600); // Starts the serial communication
lcd.begin(16, 2); // Starts the lcd communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
//Converting the distance to cm
double cdist = duration / 29 / 2;
//Finding volume of the water
double volume_of_rem_height = 3.14*3*3*(cdist);
Serial.println(1800-volume_of_rem_height);
//I WANT ONLY THE BELOW STATEMENT TO BE RUN ONCE. REST ALL SHOULD CONTINUE
//TO LOOP.
lcd.print(1800-volume_of_rem_height);
The Arduino ruleas are easy:
Everything you want to be done exactly once when the Arduino is switched on, needs to go into init
Everything else neeeds to go into loop(at least, into loop's callchain.)
If you want something not done on every iteration on loop, stop the Arduino doing that by explicitly checking
Init two values of variables in init:
currentValue = lastValue = 0;
In loop, check whether something has changed from the last iteration, if yes, do the display update:
currentValue = 1800 - volume_of_rem_height
if (currentValue != lastValue) {
lastValue = currentValue;
lcd.print (currentValue)
}
This will only do the print once currentValue has really changed from the last iteration.

Pulse generation and readout on arduino

Currently I'm working on a project where I have to read out pulses from a Arduino and check if the result is High or Low.
I had to write my own code to generate the high/low output from the Arduino:
//Pulse Generator Arduino Code
int potPin = 2; // select the input pin for the knob
int outputPin = 13; // select the pin for the output
float val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(outputPin, OUTPUT); // declare the outputPin as an OUTPUT
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin); // read the value from the k
val = val/1024;
digitalWrite(outputPin, HIGH); // sets the output HIGH
delay(val*1000);
digitalWrite(outputPin, LOW); // sets the output LOW
delay(val*1000);
}
It uses a knob to change the delay between the pulses.
Im currently trying to read the high/low data with another Arduino (Lets call this one the "count Arduino") by simply connecting the 2 with the a cable from the "outputPin" to a port on the count Arduino.
I'm using digitalRead to read the port without any delay.
//Count Arduino Code
int sensorPin = 22;
int sensorState = 0;
void setup() {
pinMode(sensorPin, INPUT);
Serial.begin(9600);
}
void loop(){
sensorState = digitalRead(sensorPin);
Serial.println(sensorState);
}
First it tried with a pulse every 1 second but the result was a spam of a ton of lows and highs. Always 3 Lows and 3 highs and repeating. It wasn’t even close to one every 1 second but more like 1 every 1 millisecond.
I cant figure out what i'm doing wrong. Is it timing issue or is there a better way to detect these changes?
a spam of a ton of lows and highs
... happens if the GND of the two Arduinos are not connected.
Also, your reading arduino prints at every loop cycle, which were a few microseconds only, if the Serial buffer would not overflow.
Better printout changes only, or use a led to show what's happening.
void loop(){
static bool oldState;
bool sensorState = digitalRead(sensorPin);
if (sensorState != oldState) {
Serial.println(sensorState);
oldState = sensorState;
}
}

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