Arduino button LED not working - arduino

When I push the button it turns the KY008 off but when I click it again it won't turn it off, but if I jiggle the Laser Diode a little bit the KY008 turns back on.
Code:
int LED = 12;
int BUTTON = 4;
void setup(){
pinMode(LED,OUTPUT);
pinMode(BUTTON,INPUT);
}
void loop(){
if(digitalRead(BUTTON) == HIGH){
digitalWrite(LED,HIGH);
}else{
digitalWrite(LED,LOW);
}
}

If you use INPUT you need to have a physical pullup (or pulldown) resistor (typically 10k).
Otherwise use INPUT_PULLUP to use the Arduino internal pullup resistors
pinMode(BUTTON, INPUT_PULLUP);
Make sure that your button closes the circuit to ground when pressed.
Also when reading a button you will have a lot of bouncing.
The easiest way to prevent the bouncing is to add a delay between reads.
void loop(){
if(digitalRead(BUTTON) == HIGH){
digitalWrite(LED,HIGH);
}else{
digitalWrite(LED,LOW);
}
delay(100);
}

Related

How to prevent an initial HIGH output in PIR Sensor

I am testing out the HC-SR501 PIR sensor on arduino. I tried a simple code tutorial online
int buzz = 13;
int pir = 2;
int value = 0;
int pirState = LOW;
void setup() {
pinMode(buzz, OUTPUT);
pinMode(pir, INPUT);
Serial.begin(9600);
}
void loop() {
delay(5000);
value = digitalRead(pir);
if (value == HIGH) {
digitalWrite(buzz, HIGH);
if (pirState == LOW) {
Serial.println("Motion Detected!");
pirState = HIGH;
}
} else {
digitalWrite(buzz, LOW);
if (pirState == HIGH){
Serial.println("Motion Ended!");
pirState = LOW;
}
}
}
This works, however, I'm trying to initialize it to a LOW output. When I first turn on the board, it initially gives me a high output, and so the buzzer activates instantly, even though I placed it away from myself. The serial prints out Motion Detected. I tried adding a delay, however it still gives out a HIGH output afterwards. Anyone knows how to solve this?
Thank you!
The pinMode sets the pin as an output, but the default state is LOW, so there should be no problem with it.
However, pin 13 is wired to onboard LED. And the onboard LED is also used by bootloader to signal its activity after the reset. You should check other pins but 13.

Arduino infiniti tone

I am making a smoke detector.
When it detects smoke iz should alert with buzzer.
Is there any way I could make it to buzz forever until external interupt such as restart pin?
Or could I just remove timing from tone() function.
Here is the code I use.
int sensorValue;
int digitalValue;
int green = 8;
int red = 7;
void setup(){
Serial.begin(9600);
pinMode( 0, INPUT);
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
}
void start(){
digitalWrite(green, HIGH);
}
void loop() {
sensorValue = analogRead(0);
digitalValue = digitalRead(0);
Serial.println(sensorValue,DEC);
Serial.println(digitalValue,DEC);
delay(2000);
if(analogRead(0) < 100){
tone(9,200,1000);
digitalWrite(red,HIGH);
}
}
Playing a sound "forever" is straightforward:
if(analogRead(A0) < 100 ) {
tone(9,2000); // once triggered, will play the sound forever
}
To switch it off, you seem to like the RESET button. So there's no need to ever call
noTone(9);
BTW: what about reading the reference ?
There is lots of ways:
Change your logic that activate the buzzer.
while (analogRead(0) < 100){
tone(9,200,1000);
}
Just use an infinite loop:
while (1) {
tone(9,200,1000);
}
Reset the Arduino to get out of the infinite loop.
An variation on this would be to replace (1) with the code that checks a pin to exit the loop or reads the sensor.
if you're really bent on using interrupts
you didn't specify what board you're working with but
for uno the 2 3 pins can be attached as interrupts and just trigger a function that turns off the tone
check out this:
attachinterrupt

Reading button inputs on arduino

I've just started tinkering with the arduino and i'm getting my head around the basics. I have a push button hooked up so so i get a serial print when it's pushed.
int button = 3;
void setup() {
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);
}
void loop() {
if (digitalRead(button) == LOW) {
Serial.print("pressed\n");
}
}
Now when the button is pressed it'll print pressed a bunch until released. Now my next step is hook up an LED and I want to use the button as a toggle. Press it the first time, it'll come on, press it a second, it'll turn off. But this will run hundreds of times while the button is pressed. How do I get around this? Thanks
int led = 5;
int button = 3;
void setup() {
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
if (digitalRead(button) == LOW) {
Serial.print("pressed\n");
toggleLed(led);
}
}
int toggleLed(int led){
if (digitalRead(led) == LOW) {
Serial.print("set on");
digitalWrite(led, HIGH);
} else {
Serial.print("set off");
digitalWrite(led, LOW);
}
}
There is an example code that comes with the Arduino IDE called the State Change Example. Study it. Basically you need to have a variable to remember the state of the button the last time you pressed it and you only react to changes in the button state. Instead of running your code anytime the button pin IS low, you run your code anytime the button pin changes from high to low.

Relay pin acts differently with load

I have the following code.
void setup(){
pinMode(14, OUTPUT);
digitalWrite(14, HIGH); //Relay
}
void loop(){
if (!digitalRead(14)){
digitalWrite(10,HIGH); //LED
digitalWrite(11,LOW); //LED
}else{
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
}
In another portion of the code the relay pin changes states and I'm monitoring that with the digitalRead portion in the loop in hopes of changing which led is on based on the state.
Now the hard part. All of that works, except when I wire the relay to a magnet. This is all for a very intricate door control system with maglocks. And for some reason with the maglock hooked up to the relay the Arduino behaves very differently. It slows to a crawl once the relay is changed. Up until then all is fine, but as soon as the relay is activated, something causes it to slow way down.
What I can't figure out is why all is fine and relay triggers without side affects, until a load is attached to it.
Any ideas? Or a better way of monitoring a relay state? (Without storing its pseudo value in a variable)
You set the pin 14 as OUTPUT, but you're trying to read from it with digitalRead.
What you want to know is the value of the register that stores the value of the port.
You could go the easy way and use an auxiliar variable that stores the pin state like this:
bool state = true;
void setup(){
pinMode(14, OUTPUT);
digitalWrite(14, state); //Relay
}
void loop(){
if (!state){
digitalWrite(10,HIGH); //LED
digitalWrite(11,LOW); //LED
}else{
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
}
And edit the rest of the code accordingly so state changes accordingly.
The 'Hardest to understand' solution, is to read the register value. since you're using pin 14 (the same as pin A0) you have to look into the port C According to the Arduino Reference on port manipulation (Link at the end of my answer).
So you can just do this:
void setup(){
pinMode(14, OUTPUT);
digitalWrite(14, HIGH); //Relay
}
void loop(){
if (!BitRead(PORTC,0)){ //Reads bit 0 of the register of PORTC (wich is the state of pin14)
digitalWrite(10,HIGH); //LED
digitalWrite(11,LOW); //LED
}else{
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
}
This solution is more elegant and is exactly what you need. It might be harder to come up with so if you don't remember this in the future you could always use the "state" variable method.
Reference Bit Read Operation and Arduino Reference on port manipulation for more information.

Arduino IR Sensors Not Calling Functions

With this code written in the Arduino IDE I used to be able to call functions based on which Infrared sensor detected an object(right, left, or both). I altered the code a little bit lately but I've continued to make sure it wasn't missing any key components which made it work in the first place. I'm not sure if it's an error in my program or hardware but now, no matter what sensor detects an object, my robot just turns slightly to the left.
//Include Servo Library
#include <Servo.h>
//Define Pins for the Sensors and LEDs
#define LsensorPin 8
#define RsensorPin 5
#define LeftTransPin 7
#define RightTransPin 6
I'm using continuous rotation servos so using 0's and 180's is how I would control them at full speed. I checked the numbers over and over but they don't seem to be the problem.
//Constants for driving servo
const int RForward = 0;
const int RBackward = 180;
const int LForward = RBackward;
const int LBackward = RForward;
const int RNeutral = 90;
const int LNeutral = 90; //0 Speed
//Name the Servos for Left and Right Motor
Servo LMotor;
Servo RMotor;
The following 2 functions, when called, transmit an infrared signal through the transmitters. I do not think this is related to the problem because the sensors still detect objects, but my robot just doesn't respond the way I want it to.
//Take Reading from Left IR Sensor
void LeftIRled(){
for(int i=0;i<=384;i++){
digitalWrite(LeftTransPin, HIGH);
delayMicroseconds(13);
digitalWrite(LeftTransPin, LOW);
delayMicroseconds(13);
}
}
//Take Reading from Right IR Sensor
void RightIRled(){
for(int i=0;i<=384;i++){
digitalWrite(RightTransPin, HIGH);
delayMicroseconds(13);
digitalWrite(RightTransPin, LOW);
delayMicroseconds(13);
}
}
These functions control the direction in which the robot moves. I've checked it many times but this could possibly be one of the problems. They basically just control the spin of each individual servo with the previously declared variables.
//Controlling Servo Directions with Functions
void Forward(){
RMotor.write(RForward);
LMotor.write(LForward);
}
void Backup(){
RMotor.write(RBackward);
LMotor.write(LBackward);
}
void Stop(){
RMotor.write(RNeutral);
LMotor.write(LNeutral);
}
void Left(){
RMotor.write(RForward);
LMotor.write(LNeutral);
}
void Right(){
RMotor.write(RNeutral);
LMotor.write(LForward);
}
//Declare INPUT and OUTPUT for each Pin connected
//to Arduino Uno and set them to LOW
void setup(){
pinMode(LeftTransPin, OUTPUT);
digitalWrite(LeftTransPin, LOW);
pinMode(RightTransPin, OUTPUT);
digitalWrite(RightTransPin, LOW);
//drive forward
LMotor.attach(9);
RMotor.attach(10);
Forward();
}
Although I can't pinpoint the problem I believe it has something to do with the loop function. It is here that I transmit the infrared signal, receive it, and then call various functions which control the movement of the robot.
void loop(){
/*Drive forward until you reach an obstacle on either the
right side, left side, or both together(something in front of
you). If the left sensor goes low, he turns right, if the right
sensor goes low, he turns left. if both turn on at the same time
he stops, backs up, and turns right till he is out of danger
*/
int lstate;
int rstate;
LeftIRled();
lstate = digitalRead(LsensorPin);
delay(50);
RightIRled();
rstate = digitalRead(RsensorPin);
delay(50);
if (lstate == LOW && rstate == LOW)
{
Stop();
delay(100);
Backup();
delay(1000);
Stop();
delay(100);
Right();
delay(500);
}
else if (lstate == LOW)
{
Stop();
delay(100);
Backup();
delay(1000);
Right();
delay(500);
}
else if (rstate == LOW)
{
Stop();
delay(100);
Backup();
delay(1000);
Left();
delay(500);
}
else
{
Forward();
}
}
Any help or advice is greatly appreciated!

Resources