Page 1 of 1

Vote circuit

Posted: Thu Mar 30, 2017 9:14 am
by brianuno
I finished the SparkFun tutorial series. That was cool. They are all very simple, at least superficially, but each one made me think. The example with far-reaching significance is a sketch and circuit for blinking one light with two buttons. Wow, I thought, when I first got it working. The only thing it does that can't be done with some bell wire, a battery, a light bulb, and two light switches is not light the light bulb if both buttons are pushed. What good is that, I asked? Well, try using it as a basis for counting votes, for one thing. Suppose you want to offer a choices between Democrat, or Republican. I cite these political parties because the U.S.A. is a two-party system. It would be more complex in countries with multi-party political systems. With this circuit, the voter must choose one or the other -but not both. You must read the entire comment section of the sketch, but let's look at the core of the sketch:
Code: Select all
  // Now let's use the above functions to combine them into one statement:
  
  if (((button1State == LOW) || (button2State == LOW))  // if we're pushing button 1 OR button 2
      && !                                               // AND we're NOT
      ((button1State == LOW) && (button2State == LOW))) // pushing button 1 AND button 2
                                                        // then...
  {
    digitalWrite(ledPin, HIGH);  // turn the LED on
  }
  else
  {
    digitalWrite(ledPin, LOW);  // turn the LED off
  }
That's some elegant code writing right there! I am a beginner at this, but even I can recognize good code writing, when I see it. For your convenience I am uploading the sketch with this post. The original wiring schematic is easy to figure out without pictures. What I would like to see next is writing the button decision to the Serial Monitor. I would like to see the vote count print, line-named, "one for Democrats," or "one for Republicans," and the data copied or downloaded from the Serial Monitor window and tallied. That's how voting machines work, right?