Make relay be turned on by any of 3 touch sensors - arduino

I have a relay and i want to turn on the light with it. 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.
int touchPin = 2;
int relayPin = 3;
int val = 0;
int lightON = 0;
int touched = 0;
void setup() {
Serial.begin(9600);
pinMode(touchPin, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
val = digitalRead(touchPin);
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);
}

I suggest reading up on Arduino programming so that you can fully understand what is happening in someone else's program and then modify it to suit. There are tons of resources online just waiting for you :)
This is how I would write the program:
`/*
* As I understand the relay is to turn on a light,
* should either of the touch sensors be triggered
*/
const int touchPin_1 = 2;
const int touchPin_2 = 3;
const int relayPin = 4;
bool isLightOn = false;
void setup() {
pinMode(touchPin_1, INPUT);
pinMode(touchPin_2, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int touchPin_1_val = digitalRead(touchPin_1);
int touchPin_2_val = digitalRead(touchPin_2);
if (touchPin_1_val == HIGH || touchPin_2_val == HIGH) { //if either of the sensors are triggered...
isLightOn = !isLightOn; //toggle light state
}
if (isLightOn) { //if the light state is true, or set to 'on'...
Serial.println("light on");
digitalWrite(relayPin, HIGH); //on
} else {
Serial.println("light off");
digitalWrite(relayPin, LOW); //off
}
delay(100);
}`

Tnx but this is how i wrote it and it works fine
int touchPin = 6;
int touchPin2 = 7;
int touchPin3 = 5;
int relayPin = 9;
int val = 0;
int val2 = 0;
int val3 = 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);
val2 = digitalRead(touchPin2);
val3 = digitalRead(touchPin3);
if((val == HIGH && lightON == LOW) || (val2 == HIGH && lightON == LOW) || (val3 == 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);
}

Related

How to use a button in a while loop in Arduino?

So i tried to make a little quiz, it can use a lot of improvement but i would like to have a finished project before i better it. I want the user to choose the answer by pressing a button.
My code currently looks like this.
char serialData;
int i = 1;
int redLed = 12;
int greenLed=13;
int buttonA=11;
int buttonB=10;
int buttonC=9;
int lastState = LOW;
int currentState;
void setup()
{
Serial.begin(9600);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buttonA,INPUT);
pinMode(buttonB,INPUT);
pinMode(buttonC,INPUT);
// read the state of the switch/button:
currentState = digitalRead(buttonA);
if(lastState == HIGH && currentState == LOW)
Serial.println("A");
// save the the last state
lastState = currentState;
delay(100);
}
void loop()
{
vraag1();
delay(1000);
vraag2();
delay(1000);
vraag3();
delay(1000);
vraag4();
delay(1000);
vraag5();
delay(1000);
vraag6();
}
void vraag1()
{ //1
Serial.println(" 3+3=? ");
delay(400);
Serial.println(" A) 6");
delay(200);
Serial.println(" B) 5 ");
delay(200);
Serial.println(" C) 4 ");
delay(200);
while (Serial.available() ==0){} //2-2
/*
This is the part I added to resolve the issue - but it
does not work.
*/
currentState = digitalRead(buttonA);
if(lastState == HIGH && currentState == LOW)
Serial.println("A");
lastState = currentState;
if (Serial.available())
{ //3
serialData = Serial.read();
if (serialData == '6' || serialData == 'a' || serialData == 'A')
{ //4
Serial.println(" Correct ");
i++;
digitalWrite(greenLed, HIGH);
} //4
else
{ //5
Serial.println(" Incorrect ");
digitalWrite(redLed, HIGH);
} //5
}
delay(1000);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
}
This is the code I added to try it but it doesn't work at all. Does anyone have another suggestion?
currentState = digitalRead(buttonA);
if(lastState == HIGH && currentState == LOW)
Serial.println("A");
lastState = currentState;

Implementing the function interrupt with if statements

I want to implement the function interrupt () but I don't know exactly how..In this case there is 2 for loops which can be seen in the code:I want whenever one of the 2 buttons is pressed the process inside the loop to be interrupted immediately:
void loop() {
int brightButton = digitalRead(K1);
int ldrStatus = analogRead(ldrPin);
if (brightButton == LOW && ldrStatus >= 200)
{
for (int i = 0; i < 10; i++)
{
digitalWrite(greenLed, HIGH);
tone(buzzer,400);
delay(500);
noTone(buzzer);
delay(500);
}
}
else {
digitalWrite(greenLed, LOW);
}
int tempButton = digitalRead(K2);
int valNTC = analogRead(NTC);
if (tempButton == LOW && valNTC > 512)
{
for (int i = 0; i <10; i++)
{
digitalWrite(redLed, HIGH);
tone(buzzer,450);
delay(300);
noTone(buzzer);
delay(1000);
}
}
else {
digitalWrite(redLed, LOW);
}
}
Example code from the Arduino manual:
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
Note that this will interrupt the for loop and return to it once the interrupt service routine is finished.
If you want to abort the for loop check the pin state in every loop cycle and break if you want to leave the for loop or return if you want to leave loop().
Of course this is not "immediately".

Trying to make a password using an array with arduino

I am a beginner to using the arduino and I'm stuck at a certain problem. What I have is a programm that prints numbers to the console with the press of a button. What I'm trying to get is to the point that I enter 4 numbers after which it checks if it is the same as an array of numbers that I set before.
Now the problem is that I dont know how to make the programm check if the array I entered with the buttons is the same as the one I wrote before.
int b1 = 4;
int bs1 = 0;
int b2 = 2;
int bs2 = 0;
int b3 = 3;
int bs3 = 0;
int count = 0;
int correctcode[] = {2,3,3,1};
int code[4];
void setup() {
// put your setup code here, to run once:
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode (b1, INPUT);
pinMode (b2, INPUT);
pinMode (b3, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
bs1 = digitalRead(b1);
bs2 = digitalRead(b2);
bs3 = digitalRead(b3);
if (bs1 == HIGH) {
count++;
Serial.print ("1");
delay(500);
if (count == 4){
Serial.println ("pincode ingevoerd, checking....");
}
}
if (bs2 == HIGH) {
count++;
Serial.print ("2");
delay(500);
if (count == 4){
Serial.println ("pincode ingevoerd, checking....");
}
}
if (bs3 == HIGH) {
count++;
Serial.print ("3");
delay(500);
if (count == 4){
Serial.println ("pincode ingevoerd, checking....");
}
}
}
// Button pins ( with external pulldown resistors )
const byte b1 = 4;
const byte b2 = 2;
const byte b3 = 3;
// Signal pins
const byte greenPin = 12;
const byte redPin = 13;
byte count = 0; // 0 ... 4 counted button presses
byte correctcode[4] = {2,3,3,1};
byte code[4];
void setup() {
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode (b1, INPUT);
pinMode (b2, INPUT);
pinMode (b3, INPUT);
Serial.begin(9600);
}
void loop() {
bool bs1 = digitalRead(b1);
bool bs2 = digitalRead(b2);
bool bs3 = digitalRead(b3);
if (bs1) add(1);
else if (bs2) add(2);
else if (bs3) add(3);
}
void add( byte value ) {
Serial.print (value);
code[count++] = value;
if (count == 4) {
count=0;
if (check()) correct();
else wrong();
}
else
delay(500);
}
bool check() {
for (byte i=0; i < 4; i++) {
if (code[i] != correctcode[i]) {
return false;
}
}
return true;
}
void wrong() {
Serial.println(" -> wrong !");
digitalWrite(redPin, HIGH);
delay(1000);
digitalWrite(redPin, LOW);
}
void correct() {
Serial.println(" -> correct !");
digitalWrite(greenPin, HIGH);
delay(300);
digitalWrite(greenPin, LOW);
}
Compiled, but Untested
Don't duplicate code, but create functions. This can make code easier to read.
If you do not want an autorepeat ( after 500 ms ) for your buttons, better check for state changes (Pressed -> Released)
If multiple buttons are pressed, I added a priority. You might want to discard that state instead.
For more than 3 buttons, read about arrays :)

Arduino | Everything on the X-axis works but not on the Y-axis

I want to make a simple memory game for the Arduino. But instead of buttons I want to use a joystick to select the blinking LEDs.
I am in the early stage of the project but my Y-axis won't read and I can't figure the problem out.
//Joystick
const int X_pin = A4;
const int Y_pin = A5;
int Joy_Waarde;
//leds
const int led_Rood = 13;
const int led_Groen = 12;
const int led_Blauw = 11;
const int led_Geel = 10;
//componenten
const int buzzer = 4;
int X_Value = 0;
int Y_Value = 0;
void setup() {
pinMode (led_Rood, OUTPUT);
pinMode (led_Groen, OUTPUT);
pinMode (led_Blauw, OUTPUT);
pinMode (led_Geel, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(A0));
Serial.println("Het spel is begonnen!");
}
void loop() {
X_Value = analogRead(X_pin);
Y_Value = analogRead(Y_pin);
if (X_Value == 1023) {
digitalWrite(led_Groen, HIGH);
} else {
digitalWrite(led_Groen, LOW);
}
if (X_Value == 0) {
digitalWrite(led_Blauw, HIGH);
} else {
digitalWrite(led_Blauw, LOW);
}
if (Y_Value == 1023) {
digitalWrite(led_Geel, HIGH);
} else {
digitalWrite(led_Geel, LOW);
}
if (Y_Value == 0) {
digitalWrite(led_Rood, HIGH);
} else {
digitalWrite(led_Rood, LOW);
}
}
Hopefully you guys now something about it! Thanks in advance!
It worked, It was something with the wiring!
Thank you!

Arduino/coding noobing having issues adding something to an already working sketch

Being a Arduino and coding noob, I've been running into brick walls testing ways to make this work.
I would like LED2 to blink while LED1 is on. I'm not exactly sure which control to use and where to put it.
int b1IN = 10;
int LED1 = 11;
int b2IN = 4;
int LED2 = 3;
boolean lastButton = LOW;
boolean ledOn = false;
boolean currentButton = LOW;
void setup() {
pinMode(b1IN, INPUT);
pinMode(LED1, OUTPUT);
pinMode(b2IN, INPUT);
pinMode(LED2, OUTPUT);
}
boolean debounce(boolean last){
boolean current = digitalRead(b2IN);
if (last != current){
delay (15);
current = digitalRead(b2IN);
}
return current;
}
void loop() {
currentButton = debounce(lastButton);
if (digitalRead(b1IN) == HIGH) {
digitalWrite(LED1, HIGH);
delay(20000);
digitalWrite(LED1, LOW);
}
if (lastButton == LOW && currentButton == HIGH) {
ledOn = !ledOn;
}
lastButton = currentButton;
digitalWrite(LED2, ledOn);
}

Resources