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.
Related
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.
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.
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.
I am building a chicken egg rocker “Incubator” This is my first attempt with a Arduino projects but I have programmed PLC’s for years.
I have run into problems: The top section of code (find home sequence when switched) I can’t get to run but it compiles, I am lost on this I am not sure if I missed some syntax some ware?
I wanted to build some subroutines to jump to that both sections of code could access similar to PLS’c but see this is a no no (the goto command).
I would appreciate some direction if anyone is willing. I do have a up/down cycle timer working with limit switches timer based on pot position.
The goal :
when the home switch is turned on the system will figure out where it is by moving to a limit then find home.
This may need to be done mid cycle to remove hatchlings (the trays have to be level / home to be removed).
I thought I could accomplish this using the while command to watch the switch then stay in that section of code (at home until the switch is turned off)
The verify function failed until I added extra brackets that did not look right but it would compile and load to the board.
Thanks in advance.
// breaking down the chicken code to find the bug
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int homeswPin = 4; // toggle sw for home sequence
int homelimitswPin = 5; // home limit sensor
int timer = 0; // variable to store timer
int uplimitswPin = 2; // up limit switch
int dwnlimitswPin = 3; // down limit switch
int upoutputPin = 7; // output to drive up
int upoutput2Pin = 8; // output to drive up
int dwnoutputPin = 9; // output to drive down
int dwnoutput2Pin = 10; // output to drive down
long previousMillis = 0; // will store last time LED was updated
long interval = timer; // interval at which to change 3519 x sensor value (milliseconds)
long timedown = timedown; // interval at which to change 3519 x sensor value (milliseconds
unsigned long currentMillis = millis();
void setup() { // put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(homeswPin, INPUT_PULLUP);
pinMode(homelimitswPin, INPUT);
pinMode(uplimitswPin, INPUT);
pinMode(dwnlimitswPin, INPUT);
pinMode(upoutputPin, OUTPUT);
pinMode(dwnoutputPin, OUTPUT);
digitalWrite(7, HIGH); // +up safety stop motor
digitalWrite(8, HIGH); // -up safety stop motor
digitalWrite(9, HIGH); // + dwn safety stop motor
digitalWrite(10, HIGH); // - dwn safety stop motor
}
void loop() {
{ // section 1 find home and stay there
while (digitalRead(homeswPin) == LOW); {
if // dont know where it is but need to find home or up limit
(digitalRead(homeswPin) == LOW && digitalRead(homelimitswPin) == HIGH &&
digitalRead(uplimitswPin) == HIGH && digitalRead(dwnlimitswPin) == HIGH) {
// drives motor up
digitalWrite(upoutputPin, LOW);
}
// move until home or up limit switch found
else if
(digitalRead(homelimitswPin == LOW) || digitalRead(uplimitswPin == LOW)) {
//turn motor off
(digitalWrite(upoutputPin, HIGH));
}
else if
// at up limit need to go home
(digitalRead(homeswPin) == LOW && digitalRead(uplimitswPin) == LOW) {
digitalWrite(dwnoutputPin, LOW); // drives motor down
//at home ?
digitalRead(homelimitswPin == HIGH);
digitalWrite(dwnoutputPin, HIGH);
} //turns motor off}
else if
// at down limit go home
(digitalRead(homeswPin) == LOW && digitalRead(dwnlimitswPin) == LOW) {
// drives motor up
digitalWrite(upoutputPin, LOW);
//at home
(digitalRead (homelimitswPin) == 0);
//turn motor off
digitalWrite(upoutputPin, HIGH);
}
else
// at home with home switch on stay here
(digitalRead(homeswPin) == LOW && digitalRead(homelimitswPin) == LOW);
}
}
}
There are lot of coding sytax error. In order re modify the code , can you able write pseudo code.The coding syntax not like PLC system. Let me know how you code should look like. Use variable same
FOr example:
if digitalRead(homeswPin) == LOW)
// mention what are the action to be take including read and write function for digital pins
if digitalRead(homeswPin) == HIGH)
// whats your expected answers.
Its better you put schematic of circuit. I found lot loop holes. Kindly go through arduino basic examples. How to use digitalRead functions, How to debug arduino codes lines by lines Etc
Examples found Here
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;
}
}