athletic response light trainer by arduino - 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;
}
}

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.

How to turn on led but turn off after program has started using arduino uno coding with IDE

I have a circuit with 2 proximity sensors, two leds and a toggle to start/end the program.
Sensor down (sD) turns on (ledB) and turns off (ledG) when activated.
Sensor up (sU) turns on (ledG) and turns off (ledB) when activated.
Context:
The proximity sensors sence once a piston pump has reached end of stroke which will tell the arduino to switch a
solenoid valve in the other direction, reversing the direction of the pump. (leds represent solenoid for ease of
testing)
The Problem:
When the toggle is switched to the closed position and if the pump is mid stroke it wont move until the solenoid valve has
been turned on and the pump reaches end of stroke which will activate a sensor starting the process.
I need one led/solenoid valve to light up and then shut off as soon as one of the proxy sensors are activated.
Edit:
Video of what I have and what I'm trying to achieve:
https://drive.google.com/file/d/15bgbLU_OcVZIzw9IDD_5R_cjSMGxHwGZ/view?usp=sharing
Thanks for any input...
This has me stumped
-Sam
int sD = 4;
int ledB = 2;
int ledG = 3;
int sU = 5;
int mainSwitch = 7;
int ledBin = 8;
int ledGin = 9;
void setup()
{
// put your setup code here, to run once:
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT);
pinMode(9, INPUT);
}
void loop()
{
mainSwitch = digitalRead(7);
if (mainSwitch == true) //Ends Program
{
sD = digitalRead(4);
sU = digitalRead(5);
digitalWrite(ledB, LOW);
digitalWrite(ledG, LOW);
}
if (mainSwitch == false)//Starts program
{
sD = digitalRead(4);
sU = digitalRead(5);
if (sD == false) digitalWrite(ledB, HIGH); //sD(proximity sensor) Turns on ledB turns off ledG when activated
if (sD == false) digitalWrite(ledG, LOW);
if (sU == false) digitalWrite(ledG, HIGH); //sU(poximity sensor) Turns on ledG turns off ledB when activated
if (sU == false) digitalWrite(ledB, LOW);
}
}
From what I can understand
When you use digitalRead() on the Arduino it can return either "HIGH" or "LOW"
if (sD == false)
if (sU == false)
Try changing the "false" to "LOW" and see if that works for you.
Don't forget to check here for more detailed info.
I understand you need either one or the other LED (piston) active, while the main switch is ON. You change them at the end point, and you keep them unchanged, when no end point is reached.
On startup, you have to start with an arbitrary (or predefined) direction but not with both LEDs off, as you have it now. So there's not too much missing.
And don't confuse constant pin numbers and their variable states :)
const byte ledB = 2;
const byte ledG = 3;
// other pin numbers, hardcoded usage in code
// sU = 5; sD = 4;
// mainSwitch = 7;
bool dir = true; // Direction: default = up
void setup()
{
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(7, INPUT_PULLUP);
}
void loop()
{
bool mainSwitch = digitalRead(7);
if (mainSwitch == true) // stops Program
{
digitalWrite(ledB, LOW);
digitalWrite(ledG, LOW);
}
else
{ // normal mode, one of both LED on
// Read end switches
bool sD = digitalRead(4);
bool sU = digitalRead(5);
delay(5); // debounce, if these are mechanical switches
if (sD) dir = true; // switch to Up
if (sU) dir = false; // switch to Down
// else don't change direction
if (dir) {
digitalWrite(ledG, LOW);
digitalWrite(ledB, HIGH);
}
else {
digitalWrite(ledB, LOW);
digitalWrite(ledG, HIGH);
}
}
}
I have solved my own question thanks for the help.
-Sam
int sD = 4;
int ledB = 2;
int ledG = 3;
int sU = 5;
int mainSwitch = 7;
int ledBin = 8;
int ledGin = 9;
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT);
pinMode(9, INPUT);
}
void loop()
{
mainSwitch = digitalRead(7);
if (mainSwitch == true) //Ends Program
{
sD = digitalRead(4);
sU = digitalRead(5);
digitalWrite(ledB, LOW);
digitalWrite(ledG, LOW);
}
if (mainSwitch == false)//Starts program
{
sU = digitalRead(5);
sD = digitalRead(4);
if (sU == HIGH) //Turns on ledG(down solenoid) at first start. sU(upper
sensor) in deactivated
{
ledGin = digitalRead(9);
ledBin = digitalRead(8);
if (ledBin == LOW)digitalWrite(ledG, HIGH);
}
else if (sU == LOW) //sU(upper sensor) is activated turning on ledB(down
solenoid) turning off ledG(up solenoid)
{
ledBin = digitalRead(8);
ledGin = digitalRead(9);
if (ledBin == LOW) digitalWrite(ledB, HIGH);//ledB(down solenoid) turn on
if (ledGin == HIGH) digitalWrite(ledG, LOW);//ledG(up solenoid) turn off
}
if (sD == LOW)//sD(Bottom sensor) is activated turning on ledB(down
solenoid) turning off ledG(down solenoid)
{
ledBin = digitalRead(8);
ledGin = digitalRead(9);
if (ledBin == HIGH) digitalWrite(ledB, LOW);//ledB(down solenoid) turn off
if (ledGin == LOW) digitalWrite(ledG, HIGH);//ledB(up solenoid) turn on
}
}
}.

How to light up multiple lines on an LED display?

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.

Power button IRremote

I am creating 3 LEDs that will light up by a remote. I am able to light up the LEDs individually but I need the power button to shut off all of the LEDs. How can I create a 4th case to turn off all LEDs?
#include <IRremote.h>
int RECV_PIN = 3; // the pin where you connect the output pin of TSOP4838
int led1 = 2;
int led2 = 4;
int led3 = 7;
int itsONled[] = {0,0,0,0};
/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1 12495 // code received from button A
#define code2 6375 // code received from button B
#define code3 31365 // code received from button C
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600); // you can comment this line
irrecv.enableIRIn(); // Start the receiver
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case code1:
if(itsONled[1] == 1) { // if first led is on then
digitalWrite(led1, LOW); // turn it off when button is pressed
itsONled[1] = 0; // and set its state as off
} else { // else if first led is off
digitalWrite(led1, HIGH); // turn it on when the button is pressed
itsONled[1] = 1; // and set its state as on
}
break;
case code2:
if(itsONled[2] == 1) {
digitalWrite(led2, LOW);
itsONled[2] = 0;
} else {
digitalWrite(led2, HIGH);
itsONled[2] = 1;
}
break;
case code3:
if(itsONled[3] == 1) {
digitalWrite(led3, LOW);
itsONled[3] = 0;
} else {
digitalWrite(led3, HIGH);
itsONled[3] = 1;
}
break;
}
Serial.println(value); // you can comment this line
irrecv.resume(); // Receive the next value
}
}

Arduino, if else LED "stuck"

So the code is not working properply, there are two leds that wont turn off "highliten" the problem. when i run the Else part of the program. i want to turn them off in the else part. :)
include
byte ledPin[] = {8, 9, 10, 11, 12, 13}; //--------------------------------.
int ledDelay; // Del 1
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 0;
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int va;
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, INPUT);
myservo.attach(3); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
for (int x=0; x<6; x++) {
pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
}
void loop() {
int on = digitalRead(6);
if (on == HIGH)
{
myservo.attach(3);
// Here is the problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if va < 523)
{
digitalWrite(5, HIGH);
}
else if (va > 555)
{
digitalWrite(4, HIGH);
}
else
{
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
// Here is the problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
va = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(va, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(1); // waits for the servo to get there
}
else
{
myservo.detach();
digitalWrite(5, LOW);
digitalWrite(4, LOW);
ledDelay = analogRead(potPin) / 4;
if ((millis() - changeTime) > ledDelay)
{
changeLED();
changeTime = millis();
}
}
}
void changeLED() {
for (int x=0; x<6; x++)
{
digitalWrite(ledPin[x], LOW);
}
digitalWrite(ledPin[currentLED], HIGH);
currentLED += direction;
if (currentLED == 6) {direction = -1;}
if (currentLED == 0) {direction = 1;}
}
in advance Thank you!
Right at the end of the sketch you have the following line:
if (currentLED == 6) { direction = -1; }
I think, without actually running the program, that the problem is here. In the previous line you have added one to the value of currentLED and you are checking to see if you have gone off the end of the ledPin array. You change the direction but you don't reset the currentLED position to be back inside the ledPin range.
The next time changeLED is called it tries to call digitalWrite(ledPin[currentLED], HIGH); but the value of currentLED is 6, which is outside the ledPin array. The Arduino probably gets upset at this point.
I think you simply need to change the statement to check whether currentLED == 5 rather than 6. This will mean that next time that changeLED is called the last LED will be turned on and the value of currentLED will be decremented (direction == -1), keeping it inside the ledPin range.

Resources