Hallo. I want to write a program where by 12 LEDs are made HIGH using digitalWrite function and when a pushbutton is pressed down, only 4 LEDs should be HIGH and others LOW until the button is released. When the button is released, all the 8 LEDs should light again. How can I go about this please? I need help
What controller are you using?
How are the LEDs connected?
A schematic would be helpful.
Read this tuorial and you will become an expert.
After you read Jim's offer, read this to learn port manipulation. PORTD happens to control 8 LEDs on pins 0-7 at once.
Be aware you can't use Serial functions if writing to LEDs on pins 0 and 1.
Actually. It is a temote traffic signal control. This is how it is:- I am building a 4exit traffic signal with main traffic lights(that is two opposite pairs using 12 LEDs). I have developed a working code of the traffic light simulation operating on a countdown. But, I want also to remotely control the traffic light signals(Using nRF24L01 modules) whereby at the press of a push button, only one opposite pair should permit passage of vehicles while blocking the other opposite pair. This is to be used incase when an emergency vehicle wants to quickly pass the roundabout. The button press should alter the normal operation and favor the side from the the emergency vehicle comes. The challenge is, I have failed to successfully stop the normal operation of the LEDs to allow this interrupt to happen.
Add two modes of operations to the programme:
- Automatic mode
- Manual mode
This is so simple that no program and no arduino is needed. All that is needed is LEDs, resistors and a push-to-break button.
I wanted to control that remotely using nRF24L01 transceivers
Sorry, but your description in post #5 is so different to the description in post #1. My answer was for post #1. You should describe your project in more detail in your first post, otherwise you may waste everyone's time.
Perhaps you should post that and we can advise how to change it to do what you want.
So you plan to simulate the approach of the emergency vehicle with a button, then work on the remote control later?
Hallo. I have a small problem which I request for assistance please. I have developed a code and a working four way traffic light signals whose opposite paths' signals are connected on same pins. Thereofore they are categorized as group A or B. I want when I press a button connected on a different Arduino board, it should prompt GreenA LEDs to get HIGH while all others are off. When the button is pressed again, the normal loop proceeds. The communication is wireless using nRF24L01 transceivers.
On the tranmitter code, the button states can be read as I have seen them on the serial monitor. But they cant be received on the receiver side therefore this triggering cant be achieved. What can I do to receive the button state signals. Someone please check my code especially Receiver code and help me out. Am stuck for three days.
//TRANSMITTER CODE
#include<SPI.h>
#include<nRF24L01.h>
#include<RF24.h>
RF24 radio(9,10);
const byte address[6]="00001";
const int buttonPin=2;
boolean buttonState;
void setup() {
pinMode(buttonPin,INPUT_PULLUP);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
Serial.begin(9600);
}
void loop() {
buttonState=digitalRead(buttonPin);
if(buttonState==1){
const char text[]="Your Button State is HIGH";
radio.write(&text,sizeof(text));
Serial.println(text);
}
else{
const char text[]="Your Button State is LOW";
radio.write(&text,sizeof(text));
Serial.println(text);
}
radio.write(&buttonState,sizeof(buttonState));
}
//RECEIVER CODE
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10);
const byte address[6]="00001";
boolean buttonState;
const int RedA = 2;
const int YellowA = 3;
const int GreenA = 4;
const int buttonPin = 8;
// Define pin constants for opposite lights (linked to same pins)
const int RedB = 5;
const int YellowB = 6;
const int GreenB = 7;
void setup() {
pinMode(RedA,OUTPUT);
pinMode(RedB,OUTPUT);
pinMode(YellowA,OUTPUT);
pinMode(YellowB,OUTPUT);
pinMode(GreenA,OUTPUT);
pinMode(GreenB,OUTPUT);
radio.begin();
radio.openReadingPipe(0,address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if(radio.available())
{
while(radio.available()){
char text[32]="";
radio.read(&text,sizeof(text));
radio.read(&buttonState,sizeof(buttonState));
while(buttonState==HIGH){
Serial.println(buttonState);
digitalWrite(GreenA,HIGH);
digitalWrite(YellowA,LOW);
digitalWrite(RedA,LOW);
digitalWrite(GreenB,LOW);
digitalWrite(YellowB,LOW);
digitalWrite(RedB,HIGH);
delay(30000);
}
}
}
//turn red lightA series on and the opposite greenB series off for 10seconds
digitalWrite(RedA,HIGH);
digitalWrite(YellowA,LOW);
digitalWrite(GreenA,LOW);
digitalWrite(RedB,LOW);
digitalWrite(YellowB,LOW);
digitalWrite(GreenB,HIGH);
delay(10000);
//turn yellowA series on and the opposite greenB series off for 10seconds
digitalWrite(YellowA,HIGH); //A series
digitalWrite(RedA,LOW);
digitalWrite(GreenA,LOW);
digitalWrite(YellowB,LOW); //B series
digitalWrite(RedB,LOW);
digitalWrite(GreenB,HIGH);
delay(3000);
//turn GreenA series on and the opposite RedB series on for 10secons
digitalWrite(GreenA,HIGH); //A series
digitalWrite(YellowA,LOW);
digitalWrite(RedA,LOW);
digitalWrite(RedB,HIGH); //B series
digitalWrite(GreenB,LOW);
digitalWrite(YellowB,LOW);
delay(10000);
//turn yellowB series on and the opposite greenB series off for 10seconds
digitalWrite(YellowB,HIGH); //A series
digitalWrite(RedB,LOW);
digitalWrite(GreenB,LOW);
digitalWrite(YellowA,LOW); //B series
digitalWrite(RedA,LOW);
digitalWrite(GreenA,HIGH);
delay(3000);
}
Hello samuelambetsaa
I´v made a small code review.
You can use the delay() function when running a single task on the Arduino.
All below listed calls of the delay() function will block the execution of the sketch.
This code design will inhibit the sketch execution in realtime.
If you have serval tasks with a dedicated timing behaiviour it is usefull to have time slices based on millis() function.
A time keeper derived from the BlinkWithOutDelay could be the base for a time handler.
This timer() function might provide the following methodes:
- start();
- stop(),
- event();
- isRunning();
Have a nice day and enjoy coding in C++.
Line 90: delay(30000);
Line 101: delay(10000);
Line 111: delay(3000);
Line 121: delay(10000);
Thank you for for the review. I've taken note of the millis() function and BlinkWithoutDelay time handler. How and where do I incooperate them in the code please?
Hello samuelambetsaa
Take a search engine of your choice and ask the WWW for 'LED chaser +arduino' .
I have merged your topics due to them having too much overlap on the same subject matter @samuelambetsaa.
In the future, please only create one topic for each distinct subject matter and be careful not to cause them to converge into parallel discussions.
The reason is that generating multiple threads on the same subject matter can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
Thanks in advance for your cooperation.
Well, let me say something first. When dealing with device-to-device communications you should get rid of any "human readable messages" and stuck with just signals. I mean, this code doesn't make any sense to me (and please use a nicer indentation):
void loop() {
buttonState=digitalRead(buttonPin);
if(buttonState==1){
const char text[]="Your Button State is HIGH";
radio.write(&text,sizeof(text));
Serial.println(text);
}
else{
const char text[]="Your Button State is LOW";
radio.write(&text,sizeof(text));
Serial.println(text);
}
radio.write(&buttonState,sizeof(buttonState));
}
This means that you're sending a string via radio verbally describing the button state (not useful for the other peer), and then the value of button state (that's the "real" data you need for, even if a "boolean" will be sent as a byte anyway). And you continuously (many times per second!) send that button state, together with that useless message. Lastly, you here don't even take button "bouncing" in consideration. What you need to communicate is just the state change, and avoid sending sequences of zeros and ones corresponding to the physical button "bounce" by waiting for a certain amount of time (tytpically a few milliseconds like 50 or 100). Then, what the receiver should do when the button changes its state will be implemented on the receiving side.
What I mean is something like this:
//TRANSMITTER CODE
#include<SPI.h>
#include<nRF24L01.h>
#include<RF24.h>
RF24 radio(9,10);
const byte address[6]="00001";
const int buttonPin=2;
byte buttonState, prevButton = LOW;
void setup() {
pinMode(buttonPin,INPUT_PULLUP);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
Serial.begin(9600);
}
void loop() {
buttonState=digitalRead(buttonPin);
// Transition LOW to HIGH
if (buttonState == HIGH && prevButton == LOW){
Serial.println("Button State is HIGH");
radio.write('1',1);
delay(50); // Debounce
}
// Transition HIGH to LOW
if (buttonState == LOW && prevButton == HIGH){
Serial.println("Button State is LOW");
radio.write('0',1);
delay(50); // Debounce
}
prevButton = buttonState;
}
Then, you need to change something in the receiver code.
First of all, please, please indent your code. It isn't a matter of "good looking", it helps you (and us) better understand the code structure. Just press Ctrl-T on the IDe and it'll do it for you, but keep using that style next.
What you surely need to change in the receiver code is a few lines, starting from getting rid of some "while()" blocks because you'll receive just button changes and it won't happen so quickly.
Then the most interesting part to be implemented will be to stop the normal automatic mode when the emergency command has been received, and change the lights configuration. This can be done using a "finite state machine" and possibly with millis() instead of delay() if you need to (almost) immediately activate the emergency traffic lights configuration. Please note I added the word "almost" because in real life you can't suddenly put a red light for a lane where vehicles could be running across: emergency configuration should include a "grace period" while you'll show a yellow light to currently active (e.g. green light) lane if any. But for the sake of this topic, let's ignore this behaviour for the moment.
Your code has the lights states on the last section (even if I think either it misses at least one delay() on yellowA, or a incorrect description) so you just need to "pack" them up under a selection of current states (implemented by a specific variable) and then make "emergency" a priority state. So, define each state by indicating the colour of each side (A and B), and do it always listing them in the same sequence (e.g. top to bottom: red, yellow, green) just for clarity (your code shows a hardly understandable "mix" of leds). And my concern is about "transitions" to/from the emergency mode depending on the current "regular" state, but you'll handle it later I hope, let's just "force" a specific configuration once the emergency mode starts. The best approach is by drawing by hand a "finite state machine diagram" (just google it to better understand what I mean, but I strongly suggest you to learn this concept because it's very common in automation).
Just to start showing you some basics of this concept, you could use a code like this below. Please note it isn't a complete code, I don't have time to do it for you and test it before posting, so it's something you'd do by yourself (and it'll help you better understand those concepts than having a "free lunch"
), and here I just "force" a specific "emergency state" to jump to, then the standard state changes and timings will apply. I mean, it's just a basic schema you should then implement, test, and improve.
...
byte buttonState;
// "standard" machine states (played in sequence and loop, except when in emergency mode)
#define S_GREEN_B 0 // A is red, B is green
#define S_YELLOW_B 1 // A is red, B is yellow
#define S_GREEN_A 2 // A is green, B is red
#define S_YELLOW_A 3 // A is yellow, B is red
byte CurrentState = S_GREEN_B; // First state
...
void loop()
{
// Emergency mode handling
if(radio.available()) {
radio.read(&buttonState,1);
// Emergency START?
if (buttonState == HIGH) {
CurrentState = S_GREEN_B; // Forces the first emergency state
Serial.println("EMERGENCY STATE!");
// Please note here a "soft" transition from current state and
// emergency one should be needed...
}
}
// Ok, let's handle the current state
Serial.print("State code: "); Serial.println(CurrentState);
switch (CurrentState) {
case S_GREEN_B:
digitalWrite(RedA,HIGH);
digitalWrite(YellowA,LOW);
digitalWrite(GreenA,LOW);
digitalWrite(RedB,LOW);
digitalWrite(YellowB,LOW);
digitalWrite(GreenB,HIGH);
delay(10000);
// Next state
CurrentState = S_YELLOW_B;
break;
case S_YELLOW_B:
digitalWrite(RedA,HIGH);
digitalWrite(YellowA,LOW); // use HIGH if a yellow light should be lit in your country ;-)
digitalWrite(GreenA,LOW);
digitalWrite(RedB,LOW);
digitalWrite(YellowB,HIGH);
digitalWrite(GreenB,LOW);
delay(3000);
// Next state
CurrentState = S_GREEN_A;
break;
case S_GREEN_A:
digitalWrite(RedA,LOW);
digitalWrite(YellowA,LOW);
digitalWrite(GreenA,HIGH);
digitalWrite(RedB,HIGH);
digitalWrite(YellowB,LOW);
digitalWrite(GreenB,LOW);
delay(10000);
// Next state
CurrentState = S_YELLOW_A;
break;
case S_YELLOW_A:
digitalWrite(RedA,LOW);
digitalWrite(YellowA,HIGH);
digitalWrite(GreenA,LOW);
digitalWrite(RedB,HIGH);
digitalWrite(YellowB,LOW); // use HIGH if a yellow light should be lit in your country ;-)
digitalWrite(GreenB,LOW);
delay(3000);
// Next state
CurrentState = S_GREEN_B; // Back to first status
break;
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.