Hello community. Im working on some project and Im lost in coding. So, Im trying to do circuit where you press a button and LED lights up from time 1ms to 10ms length. Actually do one blink. That time you will set by using potentiometer of range 1-10. This is just simulate because after this button will be replaced by PLC (PLC send impulse and arduino will generate signal) and LED will be replaced by air piston. I already watched some tutorials about delayMicroseconds() and monostable vibrator but Im bad in coding and need some inspiration. Appreciate any help. Thanks
first read and map the values of the potentiometer (0-1023) to (1-10)μs and then store it in a variable. Now use interrupt or simply trigger it for the desired amount of time . Try for yourself we will help you
thanks,Akash
const int pot = A0; //potentiometer
const int output = 13; //led
const int trigger = 4; //button
int potValue = 0;
int potMin = 0;
int potMax = 1023;
int buttonState = 0;
int Time = 10;
void setup() {
pinMode(output, OUTPUT);
pinMode(trigger, INPUT);
digitalWrite(output, HIGH);
while (millis() < 100) {
potValue = analogRead(pot);
if (potValue > potMax) {
potMax = potValue;
}
if (potValue < potMin) {
potMin = potValue;
}
}
digitalWrite(output, LOW);
}
void loop() {
potValue = analogRead(pot);
buttonState = digitalRead(trigger);
potValue = map(potValue, potMin, potMax, 1, 10);
potValue = constrain(potValue, 1, 10);
if (digitalRead(trigger) == HIGH) {
digitalWrite(output, HIGH);
delay(Time);
digitalWrite(output, LOW);
}
else {
digitalWrite(output, LOW);
}
}
this dont work because the variable 'TIME' is constanly 10 not changer in the code and why to modify the 'potmax' and 'potmin' ? keep it simple, just read the pot value map it to 1- 10ms and store it in the variable 'TiME'
thanks, Akash
Microseconds or milliseconds?
milliseconds
ahh you edited the post ?? will the piston gets trigerred within 10 millis??
What's the max/min stuff in setup for?
I don't know how to set the potentiometer to values 1-10 and store them in a variable which I will then use
here try this
const int pot = A0; //potentiometer
const int output = 13; //led
const int trigger = 4; //button
int potValue = 0;
int potMin = 0;
int potMax = 1023;
int buttonState = 0;
int Time = 10;
void setup() {
pinMode(output, OUTPUT);
pinMode(trigger, INPUT);
}
void loop() {
potValue = analogRead(pot);
buttonState = digitalRead(trigger);
Time = map(potValue, potMin, potMax, 1, 10);
if (digitalRead(trigger) == HIGH) {
digitalWrite(output, HIGH);
delay(Time);
digitalWrite(output, LOW);
}
else {
digitalWrite(output, LOW);
}
}
yes, but i want just one blink of LED, now when im holding the button led still lights. I think that my code is completely wrong.
this happens because we take time to release the button, for simplicity just add a delay at the end of 'if'.
now it dont lights up anymore.
rookvrij:
now it dont lights up anymore.
We can't see your code.
const int pot = A0; //potentiometer
const int output = 13; //led
const int trigger = 4; //button
int potValue = 0;
int potMin = 0;
int potMax = 1023;
int buttonState = 0;
int Time = 10;
void setup() {
pinMode(output, OUTPUT);
pinMode(trigger, INPUT);
}
void loop() {
potValue = analogRead(pot);
buttonState = digitalRead(trigger);
Time = map(potValue, potMin, potMax, 1, 10);
if (digitalRead(trigger) == HIGH) {
digitalWrite(output, HIGH);
delay(Time);
digitalWrite(output, LOW);
//delay(Time);
}
else {
digitalWrite(output, LOW);
}
//delay(Time);
}
dont use the variable 'Time' use about 1 sec like :
delay(1000);
here is pic from oscilloscope when im holding button. i just want one period when i press the button
ok, i understood your problem so try this
const int pot = A0; //potentiometer
const int output = 13; //led
const int trigger = 4; //button
int potValue = 0;
int potMin = 0;
int potMax = 1023;
int buttonState = 0;
int Time = 10;
bool flag=1;
void setup() {
pinMode(output, OUTPUT);
pinMode(trigger, INPUT);
}
void loop() {
potValue = analogRead(pot);
buttonState = digitalRead(trigger);
Time = map(potValue, potMin, potMax, 1, 10);
if (digitalRead(trigger) == HIGH&&flag==1) {
digitalWrite(output, HIGH);
delay(Time);
digitalWrite(output, LOW);
flag=0;
}
else if(digitalRead(trigger)==LOW){
digitalWrite(output, LOW);
flag=1;
}
}
check if this works
oh wow, thanks!!! its working
I am glad to here the good news