How to synchronously illuminate four LED lights with analogWrite - arduino

I'm using Arduino Uno and want to light four LEDs synchronously using the analogwrite method, but they are illuminating sequentially instead. Here is my code:
int brightness = 0;
int fadeAmount = 5;
boolean first = true;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
if (first) {
// these should iluminate synchronously
analogWrite(2, brightness);
analogWrite(3, brightness);
analogWrite(4, brightness);
analogWrite(5, brightness);
} else {
// these should iluminate synchronously
analogWrite(6, brightness);
analogWrite(7, brightness);
analogWrite(8, brightness);
analogWrite(9, brightness);
}
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
if (brightness == 0) {
reverseFirst();
}
}
delay(5);
}
void reverseFirst(){
if (first) {
first = false;
} else {
first = true;
}
}

analogWrite only effectively works on pins 3,5,6,9,10,11 because they are the PWM pins. According to the analogWrite source code all other pins if used with analogWrite, will default back to digitalWrite.
So I suspect you will not be able to do proper analog control on the other pins and that might be why not all of them are switching on at the same time.
If brightness is important to you then I propose that you only use the true PWM pins. If you require more than the six pins then there are analog expander chips which you can acquire or more simpler you can have a look at something like "ShiftPWM". It is a library which uses a shift register to shift pulses to its outputs effectively allowing you to control brightness of LEDs. Keep in mind that it is no longer being maintained.

Related

Some LED fade and others will flash when all LED should flash Adrunio Nano

I am having trouble with my Arduino code and circuit. The goal is to have each LED fade one after another. This is not happening correctly. Some LED will fade up and down properly and then some will blink instead. I have been trying to troubleshoot and here is what I have done and has not fixed it.
used a different board
Swap LEDs
Used different resistors
Swapped pins that blink to pins that faded and the blink will be fading
Moved circuit to a different breadboard
Check that the code is sending the correct light level through the serial monitor
Here is a picture of my board
Here is the code:
const int BUTTON = 2; // Naming switch button pin
const int LED1 = 3; // Namin LED pin
const int LED2 = 4;
const int LED3 = 5;
const int LED4 = 6;
const int LED5 = 7;
const int LED6 = 8;
const int LED7 =9;
const int LED8 = 10;
const int LED9 = 11;
int BUTTONstate = 0; // A variable to store Button Status / Input
int brightness = 0;
int fadeAmount =5;
void setup(){
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
pinMode (BUTTON, INPUT);
Serial.begin(9600);
}
void loop() {
BUTTONstate = digitalRead(BUTTON); // Reading button status / input
if (BUTTONstate == HIGH) // Condition to check button input
{
FlashingLight();
}
else
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
digitalWrite(LED8, LOW);
digitalWrite(LED9, LOW);
}
}
void FlashingLight()
{
for ( int i = 0; i<=4;i++){
//digitalWrite(LED, LOW);
fading(LED1); //Fades
fading(LED2); // blinks
fading(LED3); //fades
fading(LED4); //fades
fading(LED5); //blinks
fading(LED6); //blinks
fading(LED7); //fades
fading(LED8); //fades
fading(LED9); //fades
delay(1000);
}
}
void fading(int val) {
//brightness =0;
//digitalWrite(LED, LOW);
analogWrite(val,brightness);
for (brightness = 0; brightness <= 150; brightness += 5) {
analogWrite(val,brightness);
delay(30);
Serial.println(brightness);
}
for (brightness = 150; brightness >= 0; brightness -= 5) {
analogWrite(val,brightness);
delay(30);
Serial.println(brightness);
}
delay(100);
//brightness =0;
}
Thank you for your help and let me know if you have any questions,
According to this Arduino.cc reference not all pins of the Arduino Nano are suitable for PWM control (using analogWrite()). On the Nano, only pins 3, 5, 6, 9, 10, 11 can be used to output a PWM signal.

Arduino sleep while digital pin is HIGH

I want to make the Arduino Pro Mini run on a 3.7v (4.2v when fully charged) LI-Ion battery.
The project is I will use an IR sensor to control the relay. Based on the IR code received, I will toggle the relay digital output pin (HIGH or LOW). Initially, the arduino is set to deep sleep mode and when it receives an External Interrupt (pin 2 on pro-mini), it will process the IR code and switch on the relay.
//Interrupts using Arduino
//Circuit Digest
#include "LowPower.h"
#include <IRremote.h>
volatile int output = LOW;
int i = 0;
#define RECV_PIN 2
volatile boolean sleepEnabled = true;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
irrecv.enableIRIn();
pinMode(RECV_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(RECV_PIN), buttonPressed1, RISING); // function for creating external interrupts at pin2 on Rising (LOW to HIGH)
}
void loop()
{
Serial.println(i);
++i;
delay(1000);
output = LOW;
digitalWrite(13, output); //Turns LED ON or OFF depending upon output value
if (sleepEnabled == true) {
Serial.println("Going to sleep");
delay(1000);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
}
delay(500);
readIR();
}
void buttonPressed1() //ISR function excutes when push button at pinD2 is pressed
{
sleepEnabled = false;
}
void readIR() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
if (results.value == 0xff4ab5) {
sleepEnabled = true;
}
}
irrecv.resume(); // Receive the next value
}
When the pro-mini goes back to sleep, everything is turned off.
How can I make the pro-mini consume minimal power while the relay pin is HIGH ?

analogWrite for LED_BUILTIN does not work

I'm trying to make the LED on the Wemos D1 mini R2 ESP8266 light up gradually.
I try this code:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
for (int i = 0; i < 200; i++){
analogWrite(LED_BUILTIN, i);
delay(10);
}
for (int i = 200; i > 0; i--){
analogWrite(LED_BUILTIN, i);
delay(10);
}
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
delay (2000);
}
And it does not work. I changed LED_BUILTIN to D3 and it works, but when I changed LED_BUILTIN to D4, it doesn't work.
analogWrite command is for sending pwm signal in different duty-cycle
as far as I know the built_in led is not capable of working in pwm mode just regular on-off
It looks like the LED is wired to D3.

How do I program digital pins on arduino uno?

I'm trying to make a microcontroller with an arduino. I am supplying with +5volt from the arduino, sending it to an NC button (so i can manually decide when to output a certain timed pulse). After the button it goes to a pin that I have set as an inPin (pin8). Then I want the program to make pin 7 HIGH(with a delay), and then it goes to a transistor.
This is the code I tried making (I know almost nothing about coding):
int ledPin = 7;
int inPin = 8;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
}
void loop()
{
if (inPin=HIGH) {
digitalWrite(ledPin, HIGH);
}
delay (500);
digitalWrite(ledPin, LOW);
}
For some reason the outPin is HIGH all the time. I remembered to hook up a resistor to GND so the digital pin would stay LOW when supposed to be LOW.
Thanks in advance!
if(inPin=HIGH) is a mistake, first of all use "==" instead of "=". ALso you need to READ input pin state: int invalue = digitalRead(inPin);
Also, all pins by default coonfigured as inputs, so you don't need use pinMode(inPin, INPUT);
After those changes your code will look like:
int ledPin = 7;
int inPin = 8;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(inPin)==HIGH) digitalWrite(ledPin, HIGH);
delay (500);
digitalWrite(ledPin, LOW);
}

LED Strings attached to Arduino programmed to light brighter one after the other repeatedly, stops working after 4 cycles.

I have made a circuit involving 3 strings of LEDs.
They are supposed to glow at a low intensity initially.
Now when pin 2 on Arduino goes HIGH momentarily, the LED strings glow brighter one by one and then stop. AND, if the pin 2 is kept HIGH, then the transition should continue as long as pin 2 is HIGH.
The problem is that if I make pin 2 HIGH permanently via a push button, the transition occurs 4 times and then it stops. No transition occurs after that even if i again make the pin 2 HIGH after some time.
The code is given below:
const int pin1 = 9;
const int pin2 = 10;
const int pin3 = 11;
const int button = 2;
int val = 0;
//int brightness = 0;
void setup(){
Serial.begin(9600);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(button, INPUT);
}
void loop(){
while(true)
{
val = digitalRead(button);
if(val == HIGH)
{
if(Serial.available())
{
long int brightness = Serial.read();
analogWrite(pin1, 255);
delay(1000);
analogWrite(pin2, 255);
delay(1000);
analogWrite(pin3, 255);
delay(1000);
analogWrite(pin1, brightness);
analogWrite(pin2, brightness);
analogWrite(pin3, brightness);
delay(1000);
}
}
}
}
If you really want to configure your dimmed brightness after each reset of your arduino, you should rearrange your source in a way that reading from the serial port doesn't block your transition, while still making sure your "brightness" has a predefined value:
// global variable with preset value
long int brightness = 64;
// loop function
// http://arduino.cc/en/pmwiki.php?n=Reference/Loop
void loop(){
// check for new brightness value
if(Serial.available())
{
int brightness = Serial.read();
}
// check for pressed button
val = digitalRead(button);
if(val == HIGH)
{
// start light cycle
analogWrite(pin1, 255);
delay(1000);
analogWrite(pin2, 255);
delay(1000);
analogWrite(pin3, 255);
delay(1000);
analogWrite(pin1, brightness);
analogWrite(pin2, brightness);
analogWrite(pin3, brightness);
delay(1000);
}
}

Resources