Hello,
I need help with Arduino code.
I want to connect a LED to PIN 13 (OUTPUT) and a button to digital PIN 2 (INPUT).
When i push the button delay 500msec and start led blinking. When o release the button, LED will be OFF.
I am beginner with arduino and i need your help to do that.
void setup(){
pinMode (13, OUTPUT);
pinMode (2, INPUT_PULLUP);
}
void loop(){
if (digitalRead(2) == LOW){
delay(500);
while (digitalRead(2) == LOW){
digitalWrite (13, HIGH);
delay(50);
digitalWrite (13, LOW);
delay(50);
}
}
}
You can fix any compilation errors that might crop up.
I don't have errors when i push verify button in software but don't work.
I have 10kΩ resistor connect to GND in digitals and 5 volt from POWER to button and then goes to PIN 2, if that helps.
Thank you
gioujazz3902:
I don't have errors when i push verify button in software but don't work.
I have 10kΩ resistor connect to GND in digitals and 5 volt from POWER to button and then goes to PIN 2, if that helps.
Thank you
gioujazz3902:
I don't have errors when i push verify button in software but don't work.
I have 10kΩ resistor connect to GND in digitals and 5 volt from POWER to button and then goes to PIN 2, if that helps.
Thank you
Change your switch wiring to active low. Throw away the resistor and connect one side of the switch to ground, the other to pin 2.
Without resistor, when i push the button arduino board reset.
What does means with that "Change your switch wiring to active low"
Thanks
Wire one side of your switch to the input pin and the other to GND. When you close the switch the pin will be taken LOW, hence "active low". The INPUT_PULLUP parameter in the pinMode() command will ensure that the pin is normally held HIGH.
Thanks for your answers, but still nothing.
Is that plan?
Can you follow this schematic? I added the Arduino's on-board LED at pin 13, and showed the internal pullup on pin 7.
Nothing connects to 5V. The sketch will look for a LOW on pin 7, when it sees it turn pin 13 HIGH to turn on the LED.
(the onboard LED is actually driven by an op-amp on the Uno, but this shows the functionality across well enough.)
Happy New Year..
CrossRoads thanks you very much, your code works. I followed your schematic Now i try to replace button with Autonics proximity sensor PR08 1.5 DP (unexpectedly works with 5 volt).
I connect BLUE from sensor to GND, BROWN to 5 volt POWER and BLACK to GND but didnt work, then to PIN 2 the same result. Why didnt work?
gioujazz3902:
Happy New Year..CrossRoads thanks you very much, your code works. I followed your schematic Now i try to replace button with Autonics proximity sensor PR08 1.5 DP (unexpectedly works with 5 volt).
I connect BLUE from sensor to GND, BROWN to 5 volt POWER and BLACK to GND but didnt work, then to PIN 2 the same result. Why didnt work?
You are really going to force me to Google the prox sensor? Please provide a link.
I am really sorry.
this is sensor
gioujazz3902:
Happy New Year..CrossRoads thanks you very much, your code works. I followed your schematic Now i try to replace button with Autonics proximity sensor PR08 1.5 DP (unexpectedly works with 5 volt).
I connect BLUE from sensor to GND, BROWN to 5 volt POWER and BLACK to GND but didnt work, then to PIN 2 the same result. Why didnt work?
How do you know that the sensor works on 5V? The minimum spec is 10V. BLACK is the output, it should be connected to an input pin.
You're definitely skating on thin ice, shrugging off the device specifications.
I made it.
void setup(){
pinMode (13, OUTPUT);
pinMode (2, INPUT);
}
void loop(){
if (digitalRead(2) == HIGH){
delay(1000);
while (digitalRead(2) == HIGH){
digitalWrite (13, HIGH);
delay(100);
digitalWrite (13, LOW);
delay(100);
}
}
}
I connect BLACK to Pin 2
Yes I agree with you so i plan to use voltage regulator 12volt to 5 volt, or voltage devider with resistors.
thanks you very much...
Terrific. Yes, you can use a voltage divider to provide the right digital input level to the Arduino.
Hello, again
I want to open close a switch for lower than 1 msec, how can i setup the time? I wrote 0.5 but nothing. I wonder if 5 volt relay can't work with time too low because is mechanical switch, could i use transistor like switch on off with same code? 12 Volt 10A are max values. I want to build a bike rev limiter
Thanks
I want to open close a switch for lower than 1 msec, how can i setup the time? I wrote 0.5
Use the micros() function for your timing and you can produce a pulse as short as 4 microseconds if you need to.
gioujazz3902:
I wonder if 5 volt relay can't work with time too low because is mechanical switch, could i use transistor like switch on off with same code? 12 Volt 10A are max values.
Yes, the mechanical inertia of a relay will limit how fast it can open or close. And it's not just the mechanical aspects, but the relay coil acts like an inductor, resisting any change in current. When you turn on the output, it takes a finite time for the current to get high enough for the magnetic field to get strong enough for the contacts to move. I'm sure that with sub-millisecond timing, the mechanical contacts haven't even started to move before you are turning it off.
Yes, a transistor can be driven by the same output and the same code, and it will react much faster. But 10 amps is going to take a significantly large transistor - it will take some care in the design and component selection phases.
Could you suggest me what type of transistor i need?
Hello again, finally my project failed. When the proximity sensor is ON and stay ON, relay must be NO for eg 40 msec and then turn NC until proximity sencor turn again ON.
When proximity is OFF relay stay NC. Any idea??