I have a relay and i want to turn on the light with it and i have 2 touch sensors but with this code i can only turn it on with 1 how can i make it work? The third is a switch but it should still work the same. I've tried and it worked with a different code. But that code was for a servo and not a relay.
int touchPin = 4;
int touchPin2 = 6; // Arduino pin connected to touch sensor's pin
int touchPin3 = 7 ;
int relayPin = 9;
int val = 0;
int lightON = 0;
int touched = 0;
void setup() {
Serial.begin(9600);
pinMode(touchPin, INPUT);
pinMode(touchPin2, INPUT);
pinMode(touchPin3, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
val = digitalRead(touchPin);
val = digitalRead(touchPin2);
val = digitalRead(touchPin3);
if(val == HIGH && lightON == LOW){
touched = 1-touched;
delay(100);
}
lightON = val;
if(touched == HIGH){
Serial.println("Light ON");
digitalWrite(relayPin, LOW);
}else{
Serial.println("Light OFF");
digitalWrite(relayPin, HIGH);
}
delay(100);
}
You need to handle all three digital inputs together. In your code
val = digitalRead(touchPin);
val = digitalRead(touchPin2);
val = digitalRead(touchPin3);
you are overwriting the first two read values by the last one. You should combine them or handle all three separately. Combining three reads to one value that will be HIGH if any is HIGH:
val = digitalRead(touchPin) || digitalRead(touchPin2) || digitalRead(touchPin3);
Separately:
int val = digitalRead(touchPin);
int val2 = digitalRead(touchPin2);
int val3 = digitalRead(touchPin3);
if ((val == HIGH || val2 == HIGH || val3 == HIGH) && lightON == LOW) {
Related
I need help. I have done some research and my little understanding of keypad scanning is that the ShiftIn value of Input Column should return zero (0) when a keypad button is pressed. Mine is only returning 255 (or 11111111) in BIN. All I need is to track the zero value when a key is pressed and then scan the keys matrix to display the pressed key. I will appreciate any help. I have added my code and schematic.
]1
const int kbdRows = 4;
const int kbdCols = 4;
int LatchIn = 2; //165 pin1
int ClockPin = 3; // 595 pin11 & 165 pin2
int DataIn = 4; //165 pin9
int LatchOut = 5; // 595 pin12
int DataOut = 6; //595 pin14
int led = 7;
int PinState = 0;
char keys[kbdRows][kbdCols] = {
{ '1','2','3','4' },
{ '5','6','7','8' },
{ '9','0','A','B' },
{ 'C','D','E','F' }
};
byte KeyIsDown() {
int row;
int col;
int rowBits;
int colBits;
rowBits = 0X10;
for (row = 0; row < kbdRows; row++) {
digitalWrite(ClockPin, LOW);
digitalWrite(LatchOut, LOW);
shiftOut(DataOut, ClockPin, LSBFIRST, rowBits);
digitalWrite(LatchOut, HIGH);
delay(5);
digitalWrite(ClockPin, HIGH);
digitalWrite(LatchIn, LOW);
delay(5);
digitalWrite(LatchIn, HIGH);
colBits = shiftIn(DataIn, ClockPin, LSBFIRST);
for (col = 0; col < kbdCols; col++) {
if (colBits==0) {
// not sure what condition to put here
byte keypressed = keys[kbdRows][kbdCols]; here
// I know this is the right stuff to return here
}
return colBits;
colBits = colBits >> 1;
}
rowBits = rowBits << 1;
}
}
void setup() {
pinMode(ClockPin, OUTPUT);
pinMode(DataOut, OUTPUT);
pinMode(DataIn, INPUT_PULLUP);
pinMode(LatchOut, OUTPUT);
pinMode(LatchIn, OUTPUT);
digitalWrite(LatchOut, HIGH);
digitalWrite(LatchIn, HIGH);
Serial.begin(9600);
digitalWrite(led, HIGH);
}
void loop() {
byte retColBit = KeyIsDown();
Serial.print("ColBit: ");
Serial.println(retColBit,BIN);
delay(500);
PinState = digitalRead(DataOut);
Serial.print("DataOut: ");
Serial.println(PinState,BIN);
delay(500);
}
I'm currently doing a project on an Arduino Uno. The project is based on receiving an IR Signal from an IR Remote and then based on the signal received, perform other operations.
The problem is that the signal gets reset every time. I want to store the value received from the IR Remote and then resets it if detects another pulse.
Here is my code :
int brojac = 0;
int pinData = 10;
unsigned long lengthHeader;
unsigned long bit;
int byteValue;
int vrime = 1000 ;
int storeValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(pinData, INPUT);
}
void loop() {
lengthHeader = pulseIn(pinData, LOW);
if (lengthHeader > 1500)
{
for (int i = 1; i <= 32; i++) {
bit = pulseIn(pinData, HIGH);
if (i > 16 && i <= 24)
if (bit > 1000)
byteValue = byteValue + (1 << (i - 17));
}
}
Serial.print("byteValue = ");
Serial.println(byteValue);
if(byteValue == 66){
digitalWrite(11,HIGH);
}
else{
digitalWrite(11,LOW);
}
delay(vrime);
byteValue = 0;
delay(250);
}
I got the answer by storing the value in a variable until a new variable is detected.
int pinData = 10;
int led = 11;
unsigned long lengthHeader;
unsigned long bit;
int byteValue;
int storeValue = 0;
int previousValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(pinData, INPUT);
pinMode(led, LOW);
}
void loop() {
lengthHeader = pulseIn(pinData, LOW);
if (lengthHeader > 1500)
{
for (int i = 1; i <= 32; i++) {
bit = pulseIn(pinData, HIGH);
if (i > 16 && i <= 24)
if (bit > 1000)
byteValue = byteValue + (1 << (i - 17));
}
}
Serial.print("byteValue = ");
Serial.println(byteValue);
**storeValue = byteValue;
if (storeValue != 0){
previousValue = storeValue;
}
Serial.print("Previous value = ");
Serial.println(previousValue);**
byteValue = 0;
delay(500);
}
I am having a problem with my relay switches. I have a 5V, four relay switch for use with an Arduino. I am trying to make it so that when I push a button, one relay goes on and then, when I press it again, the same relay goes off.
This concept works with the code when using one relay. However, the problem is that my code works for one relay and one relay only. If I change the code and make multiple variables, it will not work.
FYI, I am using an Arduino UNO R3 ATmega 328
Keep in mind that this first code does work, but only for one relay. It works when I press the button to turn it on and then pressing the button again turns it off.
const int rl1 = 7;
const int rl2 = 12;
const int rl3 = 2;
const int rl4 = 8;
const int button1 = 11;
const int button2 = 10;
const int button3 = 3;
const int button4 = 4;
int rl1State = LOW;
int rl2State = LOW;
int rl3State = LOW;
int rl4State = LOW;
int buttonState = LOW;
int lastButtonState = HIGH;
int reading;
long lastDebounceTime=0;
long debounceDelay = 50;
void setup() {
Serial.begin(9600);
pinMode(rl1, OUTPUT);
pinMode(rl2, OUTPUT);
pinMode(rl3, OUTPUT);
pinMode(rl3, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
}
void loop() {
reading = digitalRead(button1);
if(reading != lastButtonState){
lastDebounceTime = millis();
lastButtonState = reading;
}
if((millis() - lastDebounceTime) > debounceDelay){
if(buttonState != lastButtonState){
buttonState = lastButtonState;
if(buttonState == HIGH){
rl1State = !rl1State;
digitalWrite(rl1, rl1State);
}
}
}
}
I tried this code for multiple relays:
const int rl1 = 7;
const int rl2 = 12;
const int rl3 = 2;
const int rl4 = 8;
const int button1 = 11;
const int button2 = 10;
const int button3 = 3;
const int button4 = 4;
int rl1State = LOW;
int rl2State = LOW;
int rl3State = LOW;
int rl4State = LOW;
//States
int buttonState1 = LOW;
int lastButtonState1 = HIGH;
int buttonState2 = LOW;
int lastButtonState2 = HIGH;
int buttonState3 = LOW;
int lastButtonState3 = HIGH;
int buttonState4 = LOW;
int lastButtonState4 = HIGH;
//Read State
int reading1;
int reading2;
int reading3;
int reading4;
long lastDebounceTime1=0;
long debounceDelay1 = 50;
long lastDebounceTime2=0;
long debounceDelay2 = 50;
long lastDebounceTime3=0;
long debounceDelay3= 50;
long lastDebounceTime4=0;
long debounceDelay4 = 50;
void setup() {
Serial.begin(9600);
pinMode(rl1, OUTPUT);
pinMode(rl2, OUTPUT);
pinMode(rl3, OUTPUT);
pinMode(rl3, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
}
void loop() {
reading1 = digitalRead(button1);
reading2 = digitalRead(button2);
reading3 = digitalRead(button3);
reading4 = digitalRead(button4);
//Relay 1
if(reading1 != lastButtonState1){
lastDebounceTime1 = millis();
lastButtonState1 = reading1;
}
if(reading1 != lastButtonState1){
lastDebounceTime1 = millis();
lastButtonState1 = reading1;
}
if((millis() - lastDebounceTime1) > debounceDelay1){
if(buttonState1 != lastButtonState1){
buttonState1 = lastButtonState1;
if(buttonState1 == HIGH){
rl1State = !rl1State;
digitalWrite(rl1, rl1State);
}
}
}
//Relay 2
if(reading2 != lastButtonState2){
lastDebounceTime2 = millis();
lastButtonState2 = reading2;
}
if((millis() - lastDebounceTime2) > debounceDelay2){
if(buttonState2 != lastButtonState2){
buttonState2 = lastButtonState2;
if(buttonState2 == HIGH){
rl2State = !rl2State;
digitalWrite(rl2, rl2State);
}
}
}
}
I also tried to re-make all the variables for each button and relay, but it still does not work.
On another note, one of my relays does not work when put to any pin (when all the pins are connected) but it works only when one of the pins are disconnected. It's really weird. I tested the relay and it's fine and I changed the Arduino but still have the same issue.
AH! This will work here is the code and the schematics for all of you that have this problem (saw on the internet a lot do have this problem), as well THANK YOU ALL for you help in solving this matter! ENJOY!
Circuit (Please note the relay cluster I am using was not available on Fritzing)
Code:
//Buttons
int button1 = 7;
int button2 = 6;
int button3 = 4;
int button4 = 2;
//Relays
int rl1 = 13;
int rl2 = 12;
int rl3 = 11;
int rl4 = 8;
//States for Relay and Button (1)
int state1 = HIGH; // the current state of the output pin
int reading1; // the current reading from the input pin
int previous1 = LOW; // the previous reading from the input pin
//States for Relay and Button (2)
int state2 = HIGH; // the current state of the output pin
int reading2; // the current reading from the input pin
int previous2 = LOW; // the previous reading from the input pin
//States for Relay and Button (3)
int state3 = HIGH; // the current state of the output pin
int reading3; // the current reading from the input pin
int previous3 = LOW; // the previous reading from the input pin
//States for Relay and Button (4)
int state4 = HIGH; // the current state of the output pin
int reading4; // the current reading from the input pin
int previous4 = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time1 = 0; // the last time the output pin was toggled
long time2 = 0;
long time3 = 0;
long time4 = 0;
long debounce1 = 200; // the debounce time, increase if the output flickers
long debounce2 = 200;
long debounce3 = 200;
long debounce4 = 200;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(rl1, OUTPUT);
pinMode(rl2, OUTPUT);
pinMode(rl3, OUTPUT);
pinMode(rl4, OUTPUT);
}
void loop() {
reading1 = digitalRead(button1);
reading2 = digitalRead(button2);
reading3 = digitalRead(button3);
reading4 = digitalRead(button4);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
//Condition Relay 1
if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
if (state1 == HIGH)
state1 = LOW;
else
state1 = HIGH;
time1 = millis();
}
//Condition Relay 2
if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce2) {
if (state2 == HIGH)
state2 = LOW;
else
state2 = HIGH;
time2 = millis();
}
//Condition Relay 3
if (reading3 == HIGH && previous3 == LOW && millis() - time3 > debounce3) {
if (state3 == HIGH)
state3 = LOW;
else
state3 = HIGH;
time3 = millis();
}
//Condition Relay 4
if (reading4 == HIGH && previous4 == LOW && millis() - time4 > debounce4) {
if (state4 == HIGH)
state4 = LOW;
else
state4 = HIGH;
time4 = millis();
}
digitalWrite(rl1, state1);
digitalWrite(rl2, state2);
digitalWrite(rl3, state3);
digitalWrite(rl4, state4);
previous1 = reading1;
previous2 = reading2;
previous3 = reading3;
previous4 = reading4;
}
Actually in my code I'm transmitting accelerometer values as well as 4 flex sensor values. I have converted accelerometer value to 4 state as F(forward), B(backward),R(right),L(left). I am able to receive this 4 states and also I'm able to write code for these states. With these states I'm also sending flex sensor values to control servo motor remotely. But I'm not able to read those flex values as it is a varying integer. Please help me to read the flex values. I have tried Serial.read(), Serial.parseInt().
My transmitter code is:
int xpin = A0;
int x;
int ypin = A1;
int y;
int vcc=13;
const int flexpin1 = 2;
const int flexpin2 = 3;
const int flexpin3 = 4;
const int flexpin4 = 5;
int flex1[20];
int flex2[20];
int flex3[20];
int flex4[20];
int flexsum1=0;
int flexsum2=0;
int flexsum3=0;
int flexsum4=0;
char state1 = 'S';
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(flexpin1, INPUT);
pinMode(flexpin2, INPUT);
pinMode(flexpin3, INPUT);
pinMode(flexpin4, INPUT);
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(vcc, OUTPUT);
digitalWrite(vcc, HIGH);
}
void loop() {
x = analogRead(xpin);
Serial.print("xpin=");
Serial.print(x);
y = analogRead(ypin);
Serial.print("\t ypin=");
Serial.print(y);
if ( y > 415)
{
state1 = 'F';
}
else if (y < 315)
{
state1 = 'B';
}
else if (x > 410)
{
state1 = 'R';
}
else if (x < 310)
{
state1 = 'L';
}
else {
state1 = 'S';
}
// flex readings stabelized//
for(int x=0; x<25; x++)
{
flex1[x]=analogRead(flexpin1);
flex2[x]=analogRead(flexpin2);
flex3[x]=analogRead(flexpin3);
flex4[x]=analogRead(flexpin4);
flexsum1=flexsum1+flex1[x];
flexsum2=flexsum2+flex2[x];
flexsum3=flexsum3+flex3[x];
flexsum4=flexsum4+flex4[x];
delayMicroseconds(20);
}
flexsum1=flexsum1/25;
flexsum2=flexsum2/25;
flexsum3=flexsum3/25;
flexsum4=flexsum4/25;
Serial.print("\t\t\tflexsum1= ");
Serial.print(flexsum1);
Serial.print("\tflexsum2= ");
Serial.print(flexsum2);
Serial.print("\tflexsum3= ");
Serial.print(flexsum3);
Serial.print("\tflexsum4= ");
Serial.print(flexsum4);
Serial.print("\t state1=");
Serial.println(state1);
delay(200);
}
My receiving code:
#include<Servo.h>
int IN1 = 13;
int IN2 = 12;
int IN3 = 7;
int IN4 = 6;
char state1 = 'S';
Servo servo1, servo2, servo3, servo4;
int flex1, flex2, flex3, flex4;
int angle1, angle2, angle3, angle4;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
servo1.attach(9);
servo2.attach(8);
servo3.attach(10);
servo4.attach(11);
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available()) {
delay(10);
state1 = Serial.read();
Serial.print(state1);
switch (state1)
{
case 'F' : digitalWrite(IN1 , HIGH);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , HIGH);
digitalWrite(IN4 , LOW);
Serial.print("\tcar is moving forward ");
delay(100);
break;
case 'B' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , HIGH);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , HIGH);
Serial.print("\tcar is moving backward ");
delay(100);
break;
case 'L' : digitalWrite(IN1 , HIGH);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , HIGH);
Serial.print("\tcar is turning left ");
delay(100);
break;
case 'R' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , HIGH);
digitalWrite(IN3 , HIGH);
digitalWrite(IN4 , LOW);
Serial.print("\tcar is turning right");
delay(100);
break;
case 'S' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , LOW);
delay(100);
break;
}
if (flex1 >= 845)
{
flex1 = Serial.read();
angle1 = map(flex1, 848, 1000, 0, 180);
angle1 = constrain(angle1, 0, 90);
servo1.write(angle1);
Serial.print("\tangle1=");
Serial.print(angle1);
delay(200);
}
if (flex2 >= 820)
{
flex2 = Serial.read();
angle2 = map(flex2, 825, 1000, 0, 180);
angle2 = constrain(angle2, 0, 90);
servo2.write(angle2);
Serial.print("\tangle2=");
Serial.print(angle2);
delay(200);
}
if (flex3 >= 770)
{
flex3 = Serial.read();
angle3 = map(flex3, 770, 930, 0, 180);
angle3 = constrain(angle3, 90, 180);
servo3.write(angle3);
Serial.print("\tangle3=");
Serial.print(angle3);
delay(200);
}
if (flex4 >= 870)
{
flex4 = Serial.read();
angle4 = map(flex4, 875, 1020, 0, 180);
angle4 = constrain(angle4, 90, 180);
servo4.write(angle4);
Serial.print("\tangle4=");
Serial.print(angle4);
delay(200);
}
}
}
I'm trying to find a way to use one button to enable/disable commands. (actually, it's a multicolor LED i'm trying to start/stop changing colors)
My code is under here, but it does not work, if anyone could tell me what's wrong, i can't see it...
int red = 0;
int redPin = 9;
int blue = 0;
int bluePin = 11;
int green = 0;
int greenPin = 10;
int state = 0;
int stateModulo = 0;
void setup() {
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
pinMode(2, INPUT);
}
void checkButton(int var, int result) {
if (digitalRead(2) == HIGH) {
var++;
result = var%2;
}
}
void changecolor(int startColor,int endColor,int startPin,int endPin,int delayTime)
{
for (endColor = 0; endColor <= 255; endColor++)
{
checkButton(state,stateModulo);
if (stateModulo == 0) {
startColor = 255 - endColor;
analogWrite(endPin, endColor);
analogWrite(startPin, startColor);
delay(delayTime);
}
}
}
void loop() {
changecolor(red,green,redPin,greenPin,10);
changecolor(green,blue,greenPin,bluePin,10);
changecolor(blue,red,bluePin,redPin,10);
}
In the code below, result is not passed by reference, nor returned.
The same applies to var, after you have modified it you throw away any edit you have made.
void checkButton(int var, int result) {
if (digitalRead(2) == HIGH) {
var++;
result = var%2;
}
}
I can recommend you a good C++ book where to learn some language basics.
My suggestion: Thinking in C++, by Bruce Eckel.
Consider this:
//Buttons
int button1 = 7;
int button2 = 6;
int button3 = 4;
int button4 = 2;
//Relays
int rl1 = 13;
int rl2 = 12;
int rl3 = 11;
int rl4 = 8;
//States for Relay and Button (1)
int state1 = HIGH; // the current state of the output pin
int reading1; // the current reading from the input pin
int previous1 = LOW; // the previous reading from the input pin
//States for Relay and Button (2)
int state2 = HIGH; // the current state of the output pin
int reading2; // the current reading from the input pin
int previous2 = LOW; // the previous reading from the input pin
//States for Relay and Button (3)
int state3 = HIGH; // the current state of the output pin
int reading3; // the current reading from the input pin
int previous3 = LOW; // the previous reading from the input pin
//States for Relay and Button (4)
int state4 = HIGH; // the current state of the output pin
int reading4; // the current reading from the input pin
int previous4 = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time1 = 0; // the last time the output pin was toggled
long time2 = 0;
long time3 = 0;
long time4 = 0;
long debounce1 = 200; // the debounce time, increase if the output flickers
long debounce2 = 200;
long debounce3 = 200;
long debounce4 = 200;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(rl1, OUTPUT);
pinMode(rl2, OUTPUT);
pinMode(rl3, OUTPUT);
pinMode(rl4, OUTPUT);
}
void loop() {
reading1 = digitalRead(button1);
reading2 = digitalRead(button2);
reading3 = digitalRead(button3);
reading4 = digitalRead(button4);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
//Condition Relay 1
if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
if (state1 == HIGH)
state1 = LOW;
else
state1 = HIGH;
time1 = millis();
}
//Condition Relay 2
if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce2) {
if (state2 == HIGH)
state2 = LOW;
else
state2 = HIGH;
time2 = millis();
}
//Condition Relay 3
if (reading3 == HIGH && previous3 == LOW && millis() - time3 > debounce3) {
if (state3 == HIGH)
state3 = LOW;
else
state3 = HIGH;
time3 = millis();
}
//Condition Relay 4
if (reading4 == HIGH && previous4 == LOW && millis() - time4 > debounce4) {
if (state4 == HIGH)
state4 = LOW;
else
state4 = HIGH;
time4 = millis();
}
digitalWrite(rl1, state1);
digitalWrite(rl2, state2);
digitalWrite(rl3, state3);
digitalWrite(rl4, state4);
previous1 = reading1;
previous2 = reading2;
previous3 = reading3;
previous4 = reading4;
}