Arduino one button On/Off control - button

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

Related

Relay turn on by any of 3 touch sensors

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) {

Arduino-4X4 matrix keypad with I/O shift register

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

Storing the value read previously until new pulse

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

arduino interrupt variable not working

I am a beginner with arduino and I'm trying to make a sinus wave generator. Since I've recently found I can't put everything into main void loop, I'm trying to use interrupts. I have problem with changing variable inside of the interrupt (Delay), I don't know where's the mistake.
Here is my code:
int sine256[] = { //256 sin values from 0 to 2pi
};
int i = 0;
int sensorPin = 7;
int outputPin = 6;
volatile float Delay = 10000;
void setup()
{
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
pinMode(sensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(sensorPin), freq, RISING);
}
void loop()
{
analogWrite(6,sine256[i]);
i = i + 1;
if(i == 256){
i = 0;
}
Serial.println(Delay);
delayMicroseconds(Delay);
}
void freq() {
Delay = Delay/2;
}
EDIT
Try this:
int sine256[] = { //256 sin values from 0 to 2pi
};
int i = 0;
int sensorPin = 7;
int outputPin = 6;
volatile float Delay = 10000;
void setup()
{
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
pinMode(sensorPin, INPUT);
//attachInterrupt(digitalPinToInterrupt(sensorPin), freq, RISING);
}
void loop()
{
analogWrite(6,sine256[i]);
i = i + 1;
if(i == 256){
i = 0;
}
Serial.println(Delay);
freq();
delay(Delay);
}
void freq() {
Delay = Delay / 2;
}
https://www.arduino.cc/en/Reference/AttachInterrupt
Try taking a look at that.
What model are you using?
The only thing that causes me troubles now is the button; when i press it, it often respond as if i had pressed the button multiple times (2,3 or 4x).
This is my final code for now. Since the execution time for a void loop is 12 microseconds, i've calculated delay required to run a generator on 20,40 & 60Hz.
int sine256[] = { //256 sin values from 0 to 2pi (from 0 to 255)
int i = 0;
int sensorPin = 2;
volatile int outputPin = 7;
volatile float Delay = 1000;
int time1;
int time2;
void setup()
{
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
pinMode(sensorPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(sensorPin), freq, FALLING);
}
void loop()
{
//time1 = micros();
analogWrite(outputPin,sine256[i]);
i = i + 1;
if(i == 256){
i = 0;
}
//time2 = micros();
//Serial.println(time2 - time1);
delay(Delay);
}
void freq() {
outputPin = 6;
if(Delay == 0.02){
analogWrite(6,LOW);
outputPin = 7;
Delay = 1000;
}
if(Delay == 0.04){
Delay = 0.02;
}
if(Delay == 0.09){
Delay = 0.04;
}
if((Delay == 1000)&&(outputPin == 6)){
Delay = 0.09;
}
Serial.println(Delay);
}

Arduino Relay Button

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

Resources