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.
Related
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.
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
}
}
}.
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.
I am just creating a simple basic program, but I can't figure out what's going wrong.
I have set three pins as output and three pins as input. When those three pins digitalRead == HIGH they will set an LED to HIGH, but instead my LED is always staying high.
Here is my Arduino code:
int LED_Low = 4; // Red LED
int LED_Avg = 3; // Yellow LED
int LED_High = 2; // Green Led
int WaterLow = 7;
int WaterAvg = 8;
int WaterHigh = 9;
void setup() {
// Put your setup code here, to run once:
pinMode(LED_Low, OUTPUT);
pinMode(LED_Avg, OUTPUT);
pinMode(LED_High, OUTPUT);
pinMode(WaterLow, INPUT);
pinMode(WaterAvg, INPUT);
pinMode(WaterHigh, INPUT);
}
void check(){
if(digitalRead(WaterLow) == HIGH){ // If Water level is low
digitalWrite(ledLow, HIGH); // Turn on red LED indication water level is low
}
else{
digitalWrite(ledLow, LOW);
}
if(digitalRead(WaterAvg) == HIGH){ // If water level is medium
digitalWrite(ledAvg, HIGH); // Turn on yellow LED indicating water level is average
}
else{
digitalWrite(ledAvg, LOW);
}
if(digitalRead(WaterHigh) == HIGH){ //
digitalWrite(ledHigh, HIGH); //
}
else{
digitalWrite(ledHigh, LOW);
}
}
void loop() {
// Put your main code here, to run repeatedly:
check();
}
In the above image I have connected led on pin 2, 3, and 4 with 1.5 kilohm resistor and three wires in pin 7, 8, and 9 which will receive input from the 5 volt pin and turn on the LED. Accordingly, the 5 volt pin is connected to the positive terminal on the power bus and with 9.1 *2 resistors in series and then this wire connect with pin 2, 3, and 4.
I found the issue. My code was OK. It was my circuit.
The pins I declared to receive input were not connected to ground.
You can make that with the help of two cases
Define the delay
if (digitalRead(WaterLow) == HIGH) // If Water level is low
{
digitalWrite(ledLow, HIGH); // Turn red LED indication water level is low
delay(2000);
}
else
{
digitalWrite(ledLow, LOW);
}
Make a condition like this
int stateled = LOW;
int previous = LOW;
long time = 0;
long debounce = 200;
void loop()
{
stateButton = digitalRead(WaterLow);
if (stateButton == HIGH && previous == LOW && millis() - time > debounce)
{
if(stateLED == HIGH)
{
stateLED = LOW;
}
else
{
stateLED = HIGH;
}
time = millis();
}
digitalWrite(ledlow, stateLED);
previous == stateButton;
}
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;
}
}