adding results of digital reads together to get 0,1,2 etc


hello,

i working on monitoring program , device being monitored has multiplexed led's status display.    using opto-isolators isolation can mimic multiplexed led's own ground , +5v reference etc.

i want monitor these such data logger can record if these led's lit.

using mega, have fed (logic level) output optos digital inputs , every second doing serial print.   i knew work , result expected.   i 0 or 1 result digital read randomly depending on if arduino "seen" moment when multiplexed led low (high default state, pulled low opto active).

my plan run loop many counts of digital read, if loop counts 150 , sum of digital reads 150, can thats not active.   if got result of less 150, pin low , must have been active , should serial print "1".

so, global variables:

code: [select]
int countled = 0;

int leda1=0;
int ledc1=0;
int ledd1=0;
int ledb2=0;

double lastmillis0 = 0;
double lastmillis1 = 0;
double lastmillis2 = 0;


setup contains relevant code:
code: [select]
  int inmin = 30;
  int inmax = 40;
  for(int i=inmin; i<=inmax; i++)
  {
    pinmode(i, input);
  }



program loop:

code: [select]

  int leda1cnt = 0;
  int ledc1cnt = 0;
  int ledd1cnt = 0;
  int ledb2cnt = 0;

  int leda1sum = 0;
  int ledc1sum = 0;
  int ledd1sum = 0;
  int ledb2sum = 0;

  int leda1dr = digitalread(30);
  int ledc1dr = digitalread(31);
  int ledd1dr = digitalread(32);
  int ledb2dr = digitalread(33);

  leda1sum = leda1sum + leda1dr;
  ledc1sum = ledc1sum + ledc1dr;
  ledd1sum = ledd1sum + ledd1dr;
  ledb2sum = ledb2sum + ledb2dr;

  countled = countled + 1;


  if (countled > 150)
  {
    if (leda1sum < countled)
    {
      leda1 = 0;
    }
    else
    {
      leda1 = 1;
    }   

    if (ledc1sum < countled)
    {
      ledc1 = 0;
    }
    else
    {
      ledc1 = 1;
    }

    if (ledd1sum < countled)
    {
      ledd1 = 0;
    }
    else   
    {
      ledd1 = 1;
    }

    if (ledb2sum < countled)
    {
      ledb2 = 0;
    }
    else   
    {
      ledb2 = 1;
    }


  if (millis() >= (lastmillis0 + 1000))
  {
    serial.print(leda1);
    serial.print(",");
    serial.print(ledc1);
    serial.print(",");     
    serial.print(ledd1);
    serial.print(","); 
    serial.print(ledb2);
    serial.print(",");

    lastmillis0 = millis();
  }


the problem is, ever result of "1".  if hold manually pin down 0, still "1" reported.

i wondering if cannot add 1's multiple digital reads create number greater 1?

perhaps need convert digital read number 1 "maths" number 1?

ive not included full code there analogue reads, conversions, sd card writes etc in there.  apspects of works perfectly, part.

any pointers handy.

thanks :)

you're resetting sums 0 each time around loop.


Arduino Forum > Using Arduino > Programming Questions > adding results of digital reads together to get 0,1,2 etc


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support