Both those examples are just reading from / comparing the current value of Millis(), they are not changing it in any way, so you can run both functions without any problem.
ricky101:
Both those examples are just reading from / comparing the current value of Millis(), they are not changing it in any way, so you can run both functions without any problem.
You might find Robins code example of help.
Although i'm having a hard time understanding some of it. I'll try thanks.
Muridious:
I'm trying but i'm stuck, because both of them use millis(). I don't know how i could fix this.
There is nothing to fix. They can be placed one after the other inside the loop() function. You might have to change the initial "grab" of the millis value so it does it only once.
Just a guess, you want the button push to influence the blink in some way? You will also have to add logic to connect the two.
Well i plan on using the button to turn off all the functions that don't involve the button itself so that it is basically an off switch for the program.
I need them combined so that i can stop using delay() to make a led flicker. Which is needed to give off a approximate range with a ultrasonic sensor. it basically should flicker faster and faster the closer you get to an object.
ricky101:
Both those examples are just reading from / comparing the current value of Millis(), they are not changing it in any way, so you can run both functions without any problem.
You might find Robins code example of help.
EXAMPLE: previousButtonMillis += buttonInterval;
what does += do in this line?
Muridious:
Well i plan on using the button to turn off all the functions that don't involve the button itself so that it is basically an off switch for the program.
I need them combined so that i can stop using delay() to make a led flicker. Which is needed to give off a approximate range with a ultrasonic sensor. it basically should flicker faster and faster the closer you get to an object.
so, what you really want is to have an LED blink as a representation of the value taken from an ultrasonic sensor.
closer the object, the faster the blink.
you do not need debounce if you have an on/off switch. for a momentary, you could use it.
what type of switch are you using ?
Hutkikz:
Its the same as: previousButtonMillis = previousButtonMillis + buttonInterval;
search compound operators
Thanks, i was having trouble finding them
dave-in-nj:
so, what you really want is to have an LED blink as a representation of the value taken from an ultrasonic sensor.
closer the object, the faster the blink.
you do not need debounce if you have an on/off switch. for a momentary, you could use it.
what type of switch are you using ?
Yes, that is exactly what I want. I tried the button withour debounce before, but it didn't seem to work very well. So someone recommended i used debounce.
I'm using a button as a switch.
when you press it it has to turn off all the calculations for measuring the distance.
when you press it again it should turn on all the calculations for measuring distance.
This is where I'm at now, starting from scratch and using robin.ino from earlier in this topic as an example.
Note: in this the led has been replaced by a buzzer to make this useable for blind people. So instead of flashing a light faster and faster it should make a sound faster and faster, though the name won't make any difference I thought I should clarify.
const int trigger = 13;
const int echo = 12;
const int buzzer = 11;
const int button = 8;
const int buttonInterval = 300;
const int blinkDuration = 500;
int buttonState;
unsigned long previousButtonMillis;
void setup() {
pinMode(echo, INPUT);
pinMode(button, INPUT_PULLUP);
pinMode(trigger, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.begin (9600);
Serial.println("ultrasonicmeasuring.ino has been started.");
}
void loop() {
currentMillis = millis();
readButton();
if(digitalRead(button) == HIGH) {
measure();
}
}
void readButton() {
if (currentMillis - previousButtonMillis >= buttonInterval && digitalRead(button) == LOW) {
buttonState = !buttonState;
previousButtonMillis += buttonInterval;
}
}
void measure() {
}
aarg:
Great. Did you add an external resistor to replace the internal pull up? How is your switch wired?
Well it's not the result i want yet. It should just turn on or off on a button press. I shouldn't have to hold it for practical reasons.
and yes i did put a resistor in there.
This is the wiring.
I'm also currently working on the blinking without delay, and hoping to be able to reproduce it somehow for the trigger, otherwise i'd have to use delayMicroseconds() in there.