My name is Thor and i live in Sweden, i am looking for help with a code to be used in an Arduino Uno board connected to an Velleman VMA05 board.
basically i simply need the inputs (input= minus through momentane BTN to input connector) to toggle the output (Output= onboard relay pin) on and off. Preferable i want to do it by connecting six buttons through one analog input.
I need help with the code as well as how the button should be connected.
I need to get this done as it is a very important tool for us but i really dont have time to learn arduino coding from scratch, please help.
pert:
Why? Do you need to save all the other pins for a different use?
Hi
I want to leave the possibility for development but mainly i want to limit the amount of wires i have to pull around the vehicle as i already have to do a lot of that. Any way of doing it digitally with minimum wires?
//
// FILE: VellemanVMA05.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: demo
// DATE: 2015-10-28
// URL: http://forum.arduino.cc/index.php?topic=355913
//
// Released to the public domain
//
bool maychange[8] = {1,1,1,1, 1,1,1,1}; // ok I only need 6 actively but this way the indexing is easier
void setup()
{
Serial.begin(115200);
Serial.print("Start ");
Serial.println(__FILE__);
for (int i=2; i<8;i++) pinMode(i, INPUT_PULLUP); // no need for a pull up resistor
for (int i=8; i<14;i++) pinMode(i, OUTPUT); // relay pins
}
void loop()
{
for (int pin = 2; pin < 8; pin++)
{
if ( digitalRead(pin) == 0 ) // pressed // switch connected to GND
{
if (maychange[pin] == 1) // 1 = true 0 = false
{
maychange[pin] = 0;
int relay = pin + 6;
digitalWrite(relay, ! digitalRead(relay)); // toggle
}
}
else
{
maychange[pin] = 1;
}
}
delay(10); // some debounce time
// other code here
}
NOte you can use the 6 analogPins also as digital in.
code would only change a little
//
// FILE: VellemanVMA05.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00a
// PURPOSE: demo
// DATE: 2015-10-28
// URL: http://forum.arduino.cc/index.php?topic=355913
//
// Released to the public domain
//
bool maychange[8] = {1,1,1,1, 1,1,1,1}; // ok I only need 6 actively but this way the indexing is easier
void setup()
{
Serial.begin(115200);
Serial.print("Start ");
Serial.println(__FILE__);
// analog ports are digital ports 14-19 IIRC
for (int i=14; i<20;i++) pinMode(i, INPUT_PULLUP); // no need for a pull up resistor
for (int i=8; i<14;i++) pinMode(i, OUTPUT); // relay pins
}
void loop()
{
for (int i= 0; i< 6; i++)
{
int pin = i + 14;
if ( digitalRead(pin) == 0 ) // pressed // switch connected to GND
{
if (maychange[i] == 1) // 1 = true 0 = false
{
maychange[i] = 0;
int relay = i + 8;
digitalWrite(relay, ! digitalRead(relay)); // toggle
}
}
else
{
maychange[i] = 1;
}
}
delay(10); // some debounce time
// other code here
}
Six buttons on an analog input? Using resistors? The problem is resistor tolerance. Six buttons means that your largest resistor has to be accurate to 1% . It's a big ask, and it might not be stable with temperature.
To read a number of buttons, the way to go is with a "parallel in, serial out" shift register. They are five bucks. The great advantage is that you can chain these together to read any number of buttons. Even a single chip will typically have 8 inputs.
Basically, you need three pins. One pin loads the current state of the inputs into the shift register, one pin reads shift register 0, and one pin signals the chip to shift all the bits down by one. Way more reliable than a resistor network. And probably quicker than doing calculations on your analog input to work out which buttons are which.
The main disadvantage is that it takes time to read the value of a specific button - you have to shift through all the others. But it's microseconds. If some of your buttons need to be read n a particularly tight loop, you can wire them to the lower places on the register and only read as many bits as you need.