Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
STEMpedia
Published © CC BY

Touch Switch Board

This project will show you how to make a DIY touch-based switchboard.

AdvancedFull instructions provided3 hours704
Touch Switch Board

Things used in this project

Hardware components

evive
STEMpedia evive
×1
evive IoT Kit
STEMpedia evive IoT Kit
×1
Bulb
×1
Bulb Holder
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Fritzing Diagram

Code

Arduino Code for Touch Based SwitchBoard

Arduino
#include <Wire.h>
#include <Adafruit_MPR121.h>
#include <EEPROM.h>

Adafruit_MPR121 cap = Adafruit_MPR121();

uint16_t currenttouched = 0;

unsigned char pressed1 = 0;
unsigned char pressed2 =0;
unsigned char pressed3 =0;
unsigned char pressed4 =0;

boolean sw1_state = true;
boolean sw2_state = true;
boolean sw3_state = true;
boolean sw4_state = true;

unsigned char add1 = 10;
unsigned char add2 = 11;
unsigned char add3 = 12;
unsigned char add4 = 13;


void setup() {
  // put your setup code here, to run once:

 Serial.begin(9600);

  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");

 pinMode(2,OUTPUT);
 pinMode(3,OUTPUT);
 pinMode(4,OUTPUT);
 pinMode(5,OUTPUT);
 

 sw1_state = EEPROM.read(add1);
 sw2_state = EEPROM.read(add2);
 sw3_state = EEPROM.read(add3);
 sw4_state = EEPROM.read(add4);


 digitalWrite(2,sw1_state);
 digitalWrite(3,sw2_state);
 digitalWrite(4,sw3_state);
 digitalWrite(5,sw4_state);
 
}

void loop() {
  // put your main code here, to run repeatedly:
  currenttouched = cap.touched();
//-------------------------SW1-----------------------------
  if(currenttouched & _BV(0))
  {
    if(pressed1==0)
    {
      sw1_state = !(sw1_state); 
      digitalWrite(2,sw1_state);
      EEPROM.write(add1,sw1_state);
      pressed1 = 1;
    }
  }
  else if(!(currenttouched & _BV(0)))
  {
    pressed1=0;
  }
  //-----------------------SW2--------------------------------
  if( currenttouched & _BV(1))
  {
    if(pressed2==0)
    {
      sw2_state = !(sw2_state);
        digitalWrite(3,sw2_state);
         EEPROM.write(add2,sw2_state);
      pressed2 = 1;
    }
  }
  else if(!(currenttouched & _BV(1)))
  {
    pressed2 =0;
  }
  //----------------------------SW3---------------------------
   if( currenttouched & _BV(2))
  {
    if(pressed3==0)
    {
      sw3_state = !(sw3_state);
        digitalWrite(4,sw3_state);
         EEPROM.write(add3,sw3_state);
      pressed3 = 1;
    }
  }
  else if(!(currenttouched & _BV(2)))
  {
    pressed3 =0;
  }
  //---------------------------SW4------------------------------
   if( currenttouched & _BV(3))
  {
    if(pressed4==0)
    {
      sw4_state = !(sw4_state);
        digitalWrite(5,sw4_state);
         EEPROM.write(add4,sw4_state);
      pressed4 = 1;
    }
  }
  else if(!(currenttouched & _BV(3)))
  {
    pressed4 =0;
  }
 
  Serial.print("SW1 status");
  Serial.println(sw1_state);

  Serial.print("SW2 status");
  Serial.println(sw2_state);

 
  Serial.print("SW3 status");
  Serial.println(sw3_state);

  Serial.print("SW4 status");
  Serial.println(sw4_state);

 
}

evive Library

C/C++
No preview (download only).

Credits

STEMpedia
42 projects • 169 followers
STEMpedia blends theory with experiential learning by offering state-of-the-art technology, projects, tutorials, courses, and much more.

Comments