Touch Count LED Tower

About The Project

In this project, we will learn how to count the number of touches up to 5, display the count on the Serial Monitor, and light up LEDs in sequence (as an LED tower) to provide a visual effect. When the count reaches 5, it will reset to 0 on the next touch, and the LEDs will turn off.

Circuit Wiring

Program Code

C++
// www.matthewtechub.com
// touch sensor-led tower
const int touchPin = 2;  
// Pin connected to the touch sensor module
const int ledPins[] = {3, 4, 5, 6, 7}; 
// Pins connected to the LEDs
const int numLeds = 5; // Number of LEDs

int touchCount = 0; 
// Variable to count the number of touches
int lastTouchState = LOW; 
// Variable to store the last state of the touch sensor

void setup() {
  Serial.begin(9600); 
  // Initialize serial communication at 9600 baud rate
  pinMode(touchPin, INPUT);
   // Set the touch pin as an input

  // Set all LED pins as outputs
  for (int i = 0; i < numLeds; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW); 
    // Ensure all LEDs are initially off
  }
}

void loop() {
  int touchState = digitalRead(touchPin); 
  // Read the current state of the touch sensor

  // Check if the touch sensor state has changed from LOW to HIGH
  if (touchState == HIGH && lastTouchState == LOW) {
    touchCount++; 
    // Increment the touch count

    // Reset the touch count if it exceeds the number of LEDs
    if (touchCount > numLeds) {
      touchCount = 0;
    }

    // Turn on the LEDs based on the current touch count
    for (int i = 0; i < numLeds; i++) {
      if (i < touchCount) {
        digitalWrite(ledPins[i], HIGH);
      } else {
        digitalWrite(ledPins[i], LOW);
      }
    }

    Serial.print("Touch count: ");
    Serial.println(touchCount); 
    // Print the touch count to the Serial Monitor
  }

This code sets up a touch sensor and a series of LEDs. Each time the touch sensor is touched, the count increments, and LEDs light up in sequence based on the count. When the count exceeds 5, it resets to 0, turning off all LEDs. The current touch count is also printed to the Serial Monitor for debugging and monitoring purposes.

Code Explanation

C++
const int touchPin = 2;  
// Pin connected to the touch sensor module
const int ledPins[] = {3, 4, 5, 6, 7}; 
// Pins connected to the LEDs
const int numLeds = 5; 
// Number of LEDs
int touchCount = 0; 
// Variable to count the number of touches
int lastTouchState = LOW; 
// Variable to store the last state of the touch sensor

Variable Declarations

  • touchPin is assigned the pin number connected to the touch sensor.
  • ledPins is an array containing the pin numbers connected to the LEDs.
  • numLeds is the number of LEDs in the array.
  • touchCount keeps track of the number of touches detected by the touch sensor.
  • lastTouchState stores the previous state of the touch sensor to detect when the state changes from LOW to HIGH.
C++
void setup() {
  Serial.begin(9600); 
  // Initialize serial communication at 9600 baud rate
  
  pinMode(touchPin, INPUT);
  // Set the touch pin as an input
  
  // Set all LED pins as outputs
  for (int i = 0; i < numLeds; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW); 
    // Ensure all LEDs are initially off
  }
}

Setup Function

  • Serial.begin(9600); initializes serial communication at a baud rate of 9600 for debugging purposes.
  • pinMode(touchPin, INPUT); sets the touch sensor pin as an input.
  • The for loop initializes each LED pin as an output and sets them to LOW to ensure all LEDs are initially off.
C++
void loop() {
  int touchState = digitalRead(touchPin); 
  // Read the current state of the touch sensor

  // Check if the touch sensor state has changed from LOW to HIGH
  if (touchState == HIGH && lastTouchState == LOW) {
    touchCount++; 
    // Increment the touch count

    // Reset the touch count if it exceeds the number of LEDs
    if (touchCount > numLeds) {
      touchCount = 0;
    }

    // Turn on the LEDs based on the current touch count
    for (int i = 0; i < numLeds; i++) {
      if (i < touchCount) {
        digitalWrite(ledPins[i], HIGH);
      } else {
        digitalWrite(ledPins[i], LOW);
      }
    }

    Serial.print("Touch count: ");
    Serial.println(touchCount); 
    // Print the touch count to the Serial Monitor
  }

  // Update the last touch state
  lastTouchState = touchState;

  // Small delay for stability
  delay(50);
}

Loop Function

  • int touchState = digitalRead(touchPin); reads the current state of the touch sensor.
  • The if statement checks if the touch sensor state has changed from LOW to HIGH. This detects a touch event.
  • When a touch event is detected, touchCount++ increments the touch count.
  • The if (touchCount > numLeds) statement resets the touch count to 0 if it exceeds the number of LEDs.
  • The for loop turns on LEDs based on the current touch count:
  • If i < touchCount, the LED is turned on.
  • Otherwise, the LED is turned off.
  • Serial.print(“Touch count: “); and Serial.println(touchCount); print the current touch count to the Serial Monitor for debugging.
  • lastTouchState = touchState; updates the last touch state to the current state.
  • delay(50); introduces a small delay for stability and to debounce the touch sensor.

Try Yourself

Modify the program code to count up to 5 with each touch. When the count reaches 5, it should step down with each subsequent touch until it reaches 0. Update the LED sequence according to the count.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!