Go Down

Topic: Looking for help to write a code for volunteer mountain rescue vehicle (Read 1 time) previous topic - next topic

cfyra

Hi

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.

//Thor

pert

i want to do it by connecting six buttons through one analog input.
Why? Do you need to save all the other pins for a different use?

cloxart


cfyra

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?

pert

Makes sense. I was just asking because it makes the wiring and code slightly more complex for learning but not too much. Sounds like a cool project!

robtillaart

Can you tell a bit more about -  "volunteer mountain rescue vehicle"
what must we think of?

DO you have a link to the Velleman Kit?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

a digital pin version
Code: (not tested) [Select]
//
//    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
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

NOte you can use the 6 analogPins also as digital in.

code would only change a little
Code: (not tested) [Select]
//
//    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
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

With one analogPin the code looks like (not tested)
Code: (not tested) [Select]
//
//    FILE: VellemanVMA05.x.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00b
// PURPOSE: demo
//    DATE: 2015-10-28
//     URL:
//
// Released to the public domain
//

bool maychange[6] = { 0, 0, 0, 0, 0, 0};

// analog values to be tuned by proper choosing resistors.
int ar[10] = { 0, 84, 136, 153, 193, 244, 512 };

int findIndex(int raw, int ar[], int len, int margin = 3, int errValue = -1)
{
  int rv = errValue;
  for (int i = 0; i < len; i++)
  {
    if ((ar[i] - margin <= raw) && (raw <= ar[i] + margin))
    {
      rv = i;
      break;
    }
  }
  return rv;
}


void setup()
{
  Serial.begin(115200);
  Serial.print("Start ");
  Serial.println(__FILE__);

  for (int i = 8; i < 14; i++) pinMode(i, OUTPUT); // relay pins
}

void loop()
{
  int x = analogRead(A0);
  int i = findIndex(x, ar, 9);
  if (i != -1)
  {
    if (maychange[i] == 1)  // 1 = true  0 = false
    {
      maychange[i] = 0;
      int relay = i + 8;
      digitalWrite(relay, ! digitalRead(relay)); // toggle
    }
  }
  else
  {
    for (int p = 0; p < 6; p++) maychange[p] = 1;
  }
  delay(100); // some debounce time

  // other code here
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

PaulMurrayCbr

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.


http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Go Up