How to light up multiple lines on an LED display? - arduino

I am working on an LED display project and trying to show some words on the display, though I can't get continuous lines to light up. Below is the code.
int greenPin = 7;
int redPin = 6;
int stbPin = 2;
int clkPin = 3;
int aPin = 4;
int bPin = 5;
int delayTime = 1;
int i = 0;
void setup() {
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(stbPin, OUTPUT);
pinMode(clkPin, OUTPUT);
pinMode(aPin, OUTPUT);
pinMode(bPin, OUTPUT);
digitalWrite(aPin, LOW);
digitalWrite(bPin, LOW);
digitalWrite(stbPin, HIGH);
digitalWrite(clkPin, LOW);
}
void loop() {
digitalWrite(stbPin, LOW);
digitalWrite(aPin, HIGH);
digitalWrite(bPin, LOW);
twoLines(B11111111, B11111111, B00000000, B00000000, B11111111, B11111111, B00000000, B00000000);
digitalWrite(aPin, LOW);
digitalWrite(bPin, LOW);
delayMicroseconds(delayTime);
digitalWrite(stbPin, HIGH);
delayMicroseconds(delayTime);
}
void twoLines( byte br, byte dr, byte ar, byte cr, byte bg, byte dg, byte ag, byte cg) {
byte Garr[] = { ag, bg, cg, dg };
byte Rarr[] = { ar, br, cr, dr };
for ( int i = 0; i < 4; i++ ) {
for (byte mask = 11111111; mask > 0; mask >>= 1) {
digitalWrite(clkPin, LOW); // delayMicroseconds(delayTime);
if (Rarr[i] & mask) {
digitalWrite(redPin, HIGH);
} else {
digitalWrite(redPin, LOW);
}
if (Garr[i] & mask) {
digitalWrite(greenPin, HIGH);
} else {
digitalWrite(greenPin, LOW);
}
delay(2);
digitalWrite(clkPin, HIGH);
delay(2);
}
}
}
Right now, I can at most only light up two rows, but I want the capability to be able to light up three rows at once to create letters. Help of any kind is appreciated. Below is also how the LED display looks like.

For things like this first thing you do, read the data sheet of your LED and see how much current they draw. If you got like 100 LED and each draw 20 mA, that would be 2 Amps and not only Arduino can not supply it, you are risking burning your Arduino. After you figured out how much current your system needs, you gotta use a power source that can handle it. You got several options, for example a BJT is a current amplifier but it complicates the design and needs some background in electrical engineering. The other option would be using external power sources such as batteries or wall plugs. Then, you just need to buy a good adapter or voltage regulator that can handle your current need. Also, in that case you will need some sort of relay to control the leds with your Arduino. What Relay does is that it lets you turn it on and off with micro controller logic, but when it is on it uses an external power source.

Related

How to use millis on traffic light controller in Arduino

I'm doing a smart city project with my Arduino and I have a question. I have created 2 functions and one of them is the traffic light controller and I use the delay() to make them have the right delays between them. But I have a problem. I call both of the functions inside loop(), but one of them only runs when the other is finished. Is there any way to run them both? I've seen people using millis().
My code:
int smartled1 = 13;
int smartled2 = 12;
int smartled3 = 11;
int smartled4 = 10;
int smartled5 = 9;
int smartled6 = 8;
int smartled7 = 7;
int smartled8 = 6;
int smartled9 = 50;
int smartled10 = 51;
int smartled11 = 52;
int smartled12 = 53;// Pin para ligar o led
int sensorPin = A0; // Seleção do pin de entrada do LDR
int sensorValor = 0; // Variavel de armazenamento do LDR inicializada a 0
int semaforo1[]= {22, 24, 26};
int semaforo2[]= {5, 4, 3};
int semaforo3[]= {29, 31, 33};
int semaforo4[]= {28, 30, 32};
int Delayvermelho = 5000;
int Delayamarelo = 2000;
void setup() {
Serial.begin(9600); // Define a porta serie para comunicação
pinMode(smartled1, OUTPUT);
pinMode(smartled2, OUTPUT);
pinMode(smartled3, OUTPUT);
pinMode(smartled4, OUTPUT);
pinMode(smartled5, OUTPUT);
pinMode(smartled6, OUTPUT);
pinMode(smartled7, OUTPUT);
pinMode(smartled8, OUTPUT);
pinMode(smartled9, OUTPUT);
pinMode(smartled10, OUTPUT);
pinMode(smartled11, OUTPUT);
pinMode(smartled12, OUTPUT);// Define o pin do Led como saída
for (int i = 0; i < 3; i++) {
pinMode(semaforo1[i], OUTPUT);
pinMode(semaforo2[i], OUTPUT);
pinMode(semaforo3[i], OUTPUT);
pinMode(semaforo4[i], OUTPUT);
}
}
void loop() {
smart_lights();
semaforos_cruzamento();
}
void semaforos_cruzamento(){
// Making Green LED at signal 1 and red LED's at other signal HIGH
digitalWrite(semaforo1[2], HIGH);
digitalWrite(semaforo1[0], LOW);
digitalWrite(semaforo2[0], HIGH);
digitalWrite(semaforo3[0], HIGH);
digitalWrite(semaforo4[0], HIGH);
delay(Delayvermelho);
// Making Green LED at signal 1 LOW and making yellow LED at signal 1 HIGH for 2 seconds
digitalWrite(semaforo1[1], HIGH);
digitalWrite(semaforo1[2], LOW);
delay(Delayamarelo);
digitalWrite(semaforo1[1], LOW);
// Making Green LED at signal 2 and red LED's at other signal HIGH
digitalWrite(semaforo1[0], HIGH);
digitalWrite(semaforo2[2], HIGH);
digitalWrite(semaforo2[0], LOW);
digitalWrite(semaforo3[0], HIGH);
digitalWrite(semaforo4[0], HIGH);
delay(Delayvermelho);
// Making Green LED at signal 2 LOW and making yellow LED at signal 2 HIGH for 2 seconds
digitalWrite(semaforo2[1], HIGH);
digitalWrite(semaforo2[2], LOW);
delay(Delayamarelo);
digitalWrite(semaforo2[1], LOW);
// Making Green LED at signal 3 and red LED's at other signal HIGH
digitalWrite(semaforo1[0], HIGH);
digitalWrite(semaforo2[0], HIGH);
digitalWrite(semaforo3[2], HIGH);
digitalWrite(semaforo3[0], LOW);
digitalWrite(semaforo4[0], HIGH);
delay(Delayvermelho);
// Making Green LED at signal 3 LOW and making yellow LED at signal 3 HIGH for 2 seconds
digitalWrite(semaforo3[1], HIGH);
digitalWrite(semaforo3[2], LOW);
delay(Delayamarelo);
digitalWrite(semaforo3[1], LOW);
// Making Green LED at signal 4 and red LED's at other signal HIGH
digitalWrite(semaforo1[0], HIGH);
digitalWrite(semaforo2[0], HIGH);
digitalWrite(semaforo3[0], HIGH);
digitalWrite(semaforo4[2], HIGH);
digitalWrite(semaforo4[0], LOW);
delay(Delayvermelho);
// Making Green LED at signal 4 LOW and making yellow LED at signal 4 HIGH for 2 seconds
digitalWrite(semaforo4[1], HIGH);
digitalWrite(semaforo4[2], LOW);
delay(Delayamarelo);
digitalWrite(semaforo4[1], LOW);
}
void smart_lights(){
int sensorValor = analogRead(sensorPin);// Lê o valor fornecido pelo LDR
Serial.println(sensorValor);//Imprime os valores provenientes do sensor na ecrã
// Caso o valor lido na porta analógica A5 seja maior do que
// 800, acende o LED
// Ajuste o valor abaixo de acordo com o circuito
if (sensorValor < 400)
{
digitalWrite(smartled1, HIGH);
digitalWrite(smartled2, HIGH);
digitalWrite(smartled3, HIGH);
digitalWrite(smartled4, HIGH);
digitalWrite(smartled5, HIGH);
digitalWrite(smartled6, HIGH);
digitalWrite(smartled7, HIGH);
digitalWrite(smartled8, HIGH);
digitalWrite(smartled9, HIGH);
digitalWrite(smartled10, HIGH);
digitalWrite(smartled11, HIGH);
digitalWrite(smartled12, HIGH);
}
else //Caso contrário, apaga o led
{
digitalWrite(smartled1, LOW);
digitalWrite(smartled2, LOW);
digitalWrite(smartled3, LOW);
digitalWrite(smartled4, LOW);
digitalWrite(smartled5, LOW);
digitalWrite(smartled6, LOW);
digitalWrite(smartled7, LOW);
digitalWrite(smartled8, LOW);
digitalWrite(smartled9, LOW);
digitalWrite(smartled10, LOW);
digitalWrite(smartled11, LOW);
digitalWrite(smartled12, LOW);
}
}
Yes, you can use state machines.
Example:
// totally randon delays. Prime to each other.
static const unsigned char MY_EVENT_TIMEOUT = 100; // in milliseconds.
static const unsigned int HIS_EVENT_TIMEOUT = 2533; // in milliseconds.
// setup two sta=te machines. In this example both state machines will simply
// wait a bit before toggling between two states.
// state machines consist of different state values, a state variable, and some data
enum MyEventState {
my_event_state_initial, // we'll just start timing
my_event_state_1,
my_event_state_2,
/* and so on... */
};
MyEventState my_state = my_state_initial;
unsigned char my_event_timestamp; // largest my_event delay is less than 255 ms
// second state machine.
enum HisEventState {
his_event_state_iinitial, // we'll wait for some external event
his_event_state_1,
his_event_state_2,
/* more states if you need */
};
HisEventState his_state = his_state_initial;
unsigned int his_event_timestamp; // largest his_event delay is less than 65535 ms
void my_event_handler()
{
switch (my_state)
{
case my_event_state_initial:
// initialize our timestamp and go straight to state 1
my_event_timestamp = (unsigned char)millis();
my_state = my_event_state_1;
// passing though to execute next state handler immediately
case my_event_state_1:
// in real application, you'd likely CHECK for a triggering event first
// and check millis() for timeouts, etc. Using different states to
// check for time out... Note the use of subtraction of UNSIGNED
// values to avoid rollover issues altogether
// the extra cast is the correct way to to it. C++ subtraction MAY
// return an unsigned int, according to the standard. In practice, it
// does not happens for 8 and 16-bit MCUs.
// no matter what you do, do not wait, poll your input line, or
// check if there are bytes on the serial buffer, do not block.
if ((unsigned char)((unsigned char)millis() - my_event_timestamp) < MY_EVENT_TIMEOUT)
{
// not enough time has elapsed, nothing to do, so return
return;
}
my_event_timestamp = (unsigned char)millis(); // get a time stamp
my_state = my_event_state_2; // change state
// passing though to execute next state handler immediately
case my_event_state_2:
// it's always the same logic in this simple state machine,
// but you can put any logic you want here to turn one light on or off,
// check inputs, etc..
if ((unsigned char)millis() - my_event_timestamp < MY_EVENT_TIMEOUT)
{
// not enough time has elapsed, nothing to do, so return
return;
}
my_event_timestamp = (unsigned char)millis(); // get a time stamp
my_state = my_event_state_1; // change state
// we're done. the handler for state 1 will execute the next time
// loop() is called.
// This would be the place you could find an infamous goto within a
// switch blck, if timing needs to be suoer duper extra tight.
// It does happen sometimes, but rarely.
return;
}
}
void his_event_handler()
{
// this is the same logic, but with a different beat.
// since these handlers do not block for timers or events
// the handlers appear to run 'concurrently'
switch (his_state)
{
case his_event_state_initial:
// initialize our timestamp and go straight to state 1
his_event_timestamp = (unsigned int)millis();
his_state = his_event_state_1;
// passing though to execute next state handler immediately
case his_event_state_1:
if ((unsigned int)millis() - his_event_timestamp < HIS_EVENT_TIMEOUT)
{
// not enough time has elapsed, nothing to do, so return
return;
}
his_event_timestamp = (unsigned int)millis(); // get a time stamp
his_state = his_event_state_2; // change state
// passing though to execute next state handler immediately
case his_event_state_2:
if ((unsigned int)millis() - his_event_timestamp < HIS_EVENT_TIMEOUT)
{
// not enough time has elapsed, nothing to do, so return
return;
}
his_event_timestamp = (unsigned int)millis(); // get a time stamp
his_state = his_event_state_1; // change state
// we're done. the handler for state 1 will execute the next time
// loop() is called.
return;
}
}
void setup()
{
}
void loop() {
// call our concurrent state machines
my_event_handler();
his_event_handler();
/* some other non-blocking code... */
}

Arduino: Pin time inactivity/activity

The purpose of the following Arduino code is to interface with the high and low signals sent from a Raspberry Pi; the full explanation is rather complex so I'll spare you the waste in time. The signals sent from the Pi (pins 10 and 11) turn a stepper motor connected to an A4988 driver clockwise or counterclockwise. The pins that dictate this out of the Arduino are the step and direction pins (9 and 8). What I am trying to accomplish is to enable the sleepPin after 60 seconds of pin 10 and 11 inactivity.
Likewise, in the same fashion, I want to stop accepting input from pin 10 and 11 if they both read the same input signal for more than 3 seconds. I've looked up methods on how to incorporate time into Arduino script but do not know how to incorporate it in this instance.
byte directionPin = 9;
byte stepPin = 8;
byte sleepPin = 12;
byte buttonCWpin = 10;
byte buttonCCWpin = 11;
boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
long previousMillis = 0;
long interval = 1000;
void setup() {
//determines length of stepper movement
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
//moves motors clockwise or counterclockwise
pinMode(buttonCWpin, INPUT_PULLUP);
pinMode(buttonCCWpin, INPUT_PULLUP);
}
void loop() {
readButtons();
actOnButtons();
}
void readButtons() {
buttonCCWpressed = false;
buttonCWpressed = false;
if (digitalRead(buttonCWpin) == LOW) {
buttonCWpressed = true;
}
if (digitalRead(buttonCCWpin) == LOW) {
buttonCCWpressed = true;
}
}
void actOnButtons() {
if (buttonCWpressed == true) {
digitalWrite(directionPin, LOW);
for(int x = 0; x < 1; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(515);
digitalWrite(stepPin,LOW);
delayMicroseconds(515);
}
}
if (buttonCCWpressed == true) {
digitalWrite(directionPin, HIGH);
for(int x = 0; x < 1; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(515);
digitalWrite(stepPin,LOW);
delayMicroseconds(515);
}
}
}
Any help would be greatly appreciated along with any tips or concerns.
Thank You.
If you have the luxury to use pins 2 and 3 rather than pins 10 and 11 (assuming you have an Arduino Uno for instance), it could be useful to work with external interrupts.
As a solution for your first problem, here's a minimal code that should put the sleep pin high after 60 seconds of inactivity on both direction pins 2 & 3:
volatile long int last_activity;
void setup(){
attachInterrupt(2, tstamp, CHANGE);
attachInterrupt(3, tstamp, CHANGE);
pinMode(2, INPUT); // your new CW pin
pinMode(3, INPUT); // your new CCW pin
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
last_activity = millis();
}
void loop(){
if (millis() - last_activity > 60e3) {
digitalWrite(12, HIGH);
// do some other things...
}
}
void tstamp(){
last_activity = millis();
}
Now for your second problem, what exactly do you mean by "stop accepting input from pins 10 and 11"? If you only need to check that their state is the same, adding volatile long int last_common_state; in the preamble and checking for digitalRead(2) == digitalRead(3); in tstamp()'s body to update last_common_state should get you on the right track.

Udoo motion sensors

I'm building a home alarm system and I have 2 motion sensors. The problem is that only one of the sensors works and I don't know why.
The board is connected to the 12V power supply. The + connections on the sensors are connected to the VIN on the board. The ground is the same and 2 separate pins s for input from the sensors.
int senz = 3;
int senz1 = 4;
int led = 5;
int led1 = 6;
int val = 0;
int val1 = 0;
void stetup
{
pinMode(senz, INPUT);
pinMode(senz1, INPUT);
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
Serial.begin(9600);
}
void loop
{
val = digitalRead(senz);
if (val == HIGH) {
digitalWrite(led, HIGH);
Serial.println("motion");
}
if (val == LOW) {
digitalWrite(led, LOW);
Serial.println("no motion");
}
val1 = digitalRead(senz1);
if (val1 == HIGH) {
digitalWrite(led1, HIGH);
Serial.println("motion");
}
if (val1 == LOW) {
digitalWrite(led1, LOW);
Serial.println("no motion");
}
}
In case, when there are more than two sensors it's better to use I2C protocol. I have used 3 axis Accelerometer for the same purpose. This module also offers I2C compatibility and we can use wire library to read the data from these sensors.

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

athletic response light trainer by arduino

currently I am trying to develop a light response trainer by arduino , as a beginning I used 3 led and 3 push button ,the led must work randomly and when the ledx is flash the user press push bottonx and so on
of course I must use approximate sensor or something similar to be more reliable
when uploading following code all leds continuous week glow (flash) what the problem? thanks for help.
int ledselect = 0;
int led1 = 11;
int led2 = 12;
int led3 = 13;
int pb1 = 4;
int pb2 = 5;
int pb3 = 6;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(pb1, INPUT);
pinMode(pb2, INPUT);
pinMode(pb3, INPUT);
}
void loop() {
int ledselect = random(3);
switch (ledselect) {
case 0: //if ledcolor equals 0 then the led1 will turn on
digitalWrite(led1, HIGH);
if (digitalRead(pb1),HIGH)
digitalWrite(led1,LOW);
break;
case 1: //if ledcolor equals 1 then the led2 will turn on
digitalWrite(led2, HIGH);
if (digitalRead(pb2),HIGH)
digitalWrite(led2,LOW);
break;
case 2: //if ledcolor equals 2 then the led3 will turn on
digitalWrite(led3, HIGH);
if (digitalRead(pb3),HIGH)
digitalWrite(led3,LOW);
break;
}
}
There are two timing problems in this program: 1) loop executes too quickly to see the response, 2) the pushbutton is read before the person has time to respond to the led.
I recommend restructuring your loop() to have the following general structure:
Turn on an led, based on random()
delay to give the person time to respond. For example, delay(500); will wait 1/2 second (1000 / 2).
read the pushbutton, based on a second switch(ledselect) code block.
delay to give the person time to get ready for the next loop. For example, delay(1000);
thankx alot I catch it ,i change the circuit so I Connect all output's switchs and connect to pin 4 in arduino (every switch become active only if his led on)
int ledselect = 0;
int led1 = 11;
int led2 = 12;
int led3 = 13;
int pb = 4;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(pb, INPUT);
}
void loop() {
int ledselect = random(3);
switch (ledselect) {
case 0:
digitalWrite(led1, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led1,LOW);
break;
case 1:
digitalWrite(led2, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led2,LOW);
break;
case 2:
digitalWrite(led3, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led3,LOW);
break;
default:
delay(20);
break;
}
}

Resources