A multi-button detector on a single pin, and saving the results

This is a walkthrough for the second IoT Workshop I produceded for SERAS.

Recording of the workshop:

Or Walkthrough as a video on YouTube.

The code, for those that don't like typing...

Part 1

int buttonValue;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  buttonValue = analogRead(A0);
  Serial.println(buttonValue);
  delay(100);
}

Part 2

int buttonValue;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  buttonValue = analogRead(A0);

  if (buttonValue < 100) {
    prevButton = '0';
  } else if (buttonValue < 200) {
    Serial.println("A");
  } else if (buttonValue < 350) {
    Serial.println("B");
  } else if (buttonValue < 500) {
    Serial.println("C");
  } else {
    Serial.println("D");
  } 
  
  delay(100);
}

Part 3

int buttonValue;
char prevButton;


void setup()
{
  Serial.begin(9600);
  prevButton = '0';  
}

void loop()
{
  buttonValue = analogRead(A0);

  if (buttonValue < 100) {
    prevButton = '0';
  } else if (buttonValue < 200) {
    if (prevButton == '0') {
      Serial.println("A");
    }
    prevButton = 'A';
  } else if (buttonValue < 350) {
    if (prevButton == '0') {
      Serial.println("B");
    }
    prevButton = 'B';
  } else if (buttonValue < 500) {
    if (prevButton == '0') {
      Serial.println("C");
    }
    prevButton = 'C';
  } else {
    if (prevButton == '0') {
      Serial.println("D");
    }
    prevButton = 'D';
  } 
  
  delay(100);
}

Part 4

#include <EEPROM.h>

int buttonValue;
char prevButton;


void setup()
{
  Serial.begin(9600);
  prevButton = '0';  
  pinMode(5,INPUT_PULLUP);
  pinMode(6,INPUT_PULLUP);
}

void saveValue(char value) {
  bool carryOn = true;
  int index = 0;
  Serial.println("Saving value");
  do {
    if (EEPROM[index] == 0) {
      carryOn = false;
    } else {
      index = index + 1;
    }
  } while (carryOn == true and index < EEPROM.length());
  Serial.print("Index found: ");
  Serial.println(index);
  EEPROM.write(index,value);
  Serial.println("Saved");
}


void loop()
{
  if (digitalRead(5) == LOW) {
    Serial.println("Showing EEPROM");
    for (int index = 0; index < EEPROM.length(); index++) {
      if (EEPROM[index] > 0) {
        Serial.println(EEPROM[index]);
      }
    }
    Serial.println("All shown.");
  }
  if (digitalRead(6) == LOW) {
    Serial.println("Clearing EEPROM (takes some time)");
    for (int i = 0 ; i < EEPROM.length() ; i++) {
      EEPROM.write(i, 0);
    }
    Serial.println("EEPROM Cleared");
  }

  
  buttonValue = analogRead(A0);

  if (buttonValue < 100) {
    prevButton = '0';
  } else if (buttonValue < 200) {
    if (prevButton == '0') {
      Serial.println("A");
      saveValue('A');
    }
    prevButton = 'A';
  } else if (buttonValue < 350) {
    if (prevButton == '0') {
      Serial.println("B");
      saveValue('B');
    }
    prevButton = 'B';
  } else if (buttonValue < 500) {
    if (prevButton == '0') {
      Serial.println("C");
      saveValue('C');
    }
    prevButton = 'C';
  } else {
    if (prevButton == '0') {
      Serial.println("D");
      saveValue('D');
    }
    prevButton = 'D';
  } 
  
  delay(100);
}