Relay pin acts differently with load - arduino

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.

Related

How do I make pwm signals with arduino ide without useing analogwrite?

Hi can someone help with crating a pwm(or very similar) signal without analogwrite on the Arduino IDE because analogwrite wont work on the esp32.
And without using a library.
I need it to take values from 0 to 255.
I was thinking to use the second core one the esp32 but I have to control more than one pin.
you can create PWM pulse with delay or millis function. for example:
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delayMicroseconds(100);
digitalWrite(13, LOW);
delayMicroseconds(1000 - 100);
}
when you change the delay value, you can change duty-cycle level!

independent blinking lights in Arduino

I need to write a program in Arduino that turns ON or OFF two LEDs independently depending on the data received via serial communication. For example, the first LED will turn on if the Arduino board receives a1,and it will turn OFF if a2 is received. but I require help on doing this. i havent started on this yet but i am trying to do it using 2 blinking light code. I am new to Arduino programming and i don't understand some concepts. but i can definitely use help on doing this.
int LEDred = 3;
int LEDgreen = 6;
void setup() {
pinMode (LEDred, OUTPUT);
pinMode (LEDgreen, OUTPUT);
}
void loop() {
digitalWrite (LEDred, HIGH);
digitalWrite (LEDgreen, LOW);
delay (1000);
digitalWrite (LEDred, LOW);
digitalWrite (LEDgreen, HIGH);
delay (1000);
}
Use 1 or 2 instead of a1 and a2. Because it's easier to process.Consider you send 1 via serial port. Read the signal by
byte val;
if(Serial.available()>0){
val=Serial.read();
}
Then use another if statement in order to blink red or green LED

Problems with simplest Arduino program

Today I started what is supposed to become a great Arduino career, but I'm already stumped. I may be going crazy, but shouldn't this code blink the LED on the Mega 2560?
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
unsigned int count = 0;
void loop() {
if(count%2) digitalWrite(LED_BUILTIN, LOW);
else digitalWrite(LED_BUILTIN, HIGH);
delay(1);
count++;
}
I know this is not elegant for a blinking LED, but this is a stripped down example for something else, where I need a counter and modulo operations on it. The 'Blink' program works, but this here doesn't.
delay()'s argument is measured in milliseconds (not seconds), so you probably want 1000 rather than 1 to observe the blinking!
delay(1000);
Official Documentation

Arduino button LED not working

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);
}

Restarting Arduino and keeping the variables

I would like to restart the arduino board but keeping values of some variable. My solution would be calling setup() whenever I would like to restart. Something like this:
int led = 13;
int led2 = 50;
boolean restart = false;
void setup() {
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
if(!restart){
digitalWrite(led, LOW); // will only happen once
delay(3000);
}
digitalWrite(led, HIGH); // turn the LED on (and will be always on even after reset)
restart = true;
delay(3000);
digitalWrite(led2, HIGH); // indicate restart is called
delay(1000);
digitalWrite(led2, LOW);
setup();}
void loop() { }
I was thinking if this will cause any heavy usage in RAM. Or is there any better methods?
Thank you.
Use the EEPROM library. Have a button with an interrupt that saves the variable then read the variable in the setup() routine.
If you are getting or changing information slow enough you could constantly write the value but beware EEPROM on this chip is only certified to 100,000 writes per byte.

Resources