limchengwei
Published © CC BY

Multiple switches to single analogRead

This tutorial we will learn how to use multiple switches and output a single signal to be analogRead by an Arduino.

BeginnerFull instructions provided1 hour158
Multiple switches to single analogRead

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×12
Resistor 1k ohm
Resistor 1k ohm
×12

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Breadboard, 830 Tie Points
Breadboard, 830 Tie Points

Story

Read more

Schematics

Multiple switches to single analogRead using EasyEDA

Code

Multiple switches to single analogRead

Arduino
int pin = A0;

void setup()
{
  pinMode (pin, INPUT);
  Serial.begin (9600);
}

void loop()
{
  int volt = analogRead (pin);
  Serial.println(volt);
  if (volt >= 0 && volt <= 2)
  {
    Serial.println("1");
  }
  if (volt >= 510 && volt <= 514)
  {
    Serial.println("2");
  }
  if (volt >= 680 && volt <= 684)
  {
    Serial.println("3");
  }
  if (volt >= 765 && volt <= 769)
  {
    Serial.println("4");
  }
  if (volt >= 817 && volt <= 821)
  {
    Serial.println("5");
  }
  if (volt >= 851 && volt <= 855)
  {
    Serial.println("6");
  }
  if (volt >= 875 && volt <= 879)
  {
    Serial.println("7");
  }
  if (volt >= 893 && volt <= 897)
  {
    Serial.println("8");
  }
  if (volt >= 908 && volt <= 912)
  {
    Serial.println("9");
  }
  if (volt >= 919 && volt <= 923)
  {
    Serial.println("10");
  }
  if (volt >= 929 && volt <= 933)
  {
    Serial.println("11");
  }
  if (volt >= 936 && volt <= 940)
  {
    Serial.println("12");
  }
  delay (500);
}

Credits

limchengwei
24 projects • 4 followers
Contact

Comments

Please log in or sign up to comment.