Running led in another project 74hc164

Hello, i am trying to add function of using chasing led on various keypress in my code using 74HC164 , can i get some help please ?

led should run on while else if function runs , on each key apart from else
english isnt my natural language , so if something needs more explanation i will add :slight_smile:

i plan to use pin 12 as data and 13 as clock !

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <hd44780.h> //definujeme lcd
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp lcd(0x27, 16, 2);


#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme;

#define Key1 2
#define Key2 3
#define Key3 4
#define Key4 5


void setup() {
  pinMode(Key1,INPUT_PULLUP);
  pinMode(Key2,INPUT_PULLUP);
  pinMode(Key3,INPUT_PULLUP);
  pinMode(Key4,INPUT_PULLUP);
  Serial.begin(9600);
  
  lcd.init();
  lcd.clear();
  lcd.setCursor(7,0);

	if (!bme.begin(0x76)) {
		lcd.print("ERROR");
		while (1);
	}
}

void loop() {
  lcd.clear();
  int key1State = digitalRead(Key1);
  int key2State = digitalRead(Key2);
  int key3State = digitalRead(Key3);
  int key4State = digitalRead(Key4);
  
  if (key1State > 0) {
    for(int i = 0;i <=50; i++) {
  lcd.clear();
  lcd.setCursor(4,0);
	lcd.print("Teplota");
	lcd.setCursor(4,1);
	lcd.print(bme.readTemperature());
	lcd.setCursor(10,1);
	lcd.print("*C");
	delay(100);
    }
  }
  
	else if (key3State > 0) {
	  for(int i = 0;i <=50; i++) {
	lcd.clear();
  lcd.setCursor(5,0);
	lcd.print("Tlak");
	lcd.setCursor(4,1);
	lcd.print(bme.readPressure() / 100.0F);
	lcd.setCursor(11,1);
	lcd.print("hPa");
	delay(100);
	  }
	}
  
  else if (key4State > 0) {
    for(int i = 0;i <=50; i++) {
  lcd.clear();
  lcd.setCursor(4,0);
	lcd.print("Vyska nm");
	lcd.setCursor(4,1);
	lcd.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
	lcd.setCursor(11,1);
	lcd.print("m");
	delay(100);
    }
  }
	
	else if (key2State > 0) {
	  for(int i = 0;i <=50; i++) {
	lcd.clear();
  lcd.setCursor(4,0);
	lcd.print("Vlhkost");
	lcd.setCursor(5,1);
	lcd.print(bme.readHumidity());
	lcd.setCursor(11,1);
	lcd.print("%");
	delay(100);
	  }
	}
  
  else
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("____LOADING_____");
  lcd.setCursor(0,1);
  lcd.print("________________");
	delay(1000);
}

Why not do this:

  • switch to a TCIP6C595 or TPIC6B595 shift register so you can sink more than 4mA thru an LED.
  • use 13 as clock (SCLK) and 11 as data, then you use SPI.transfer() to send data to the shift register. Pin 10 as the output latch signal (RCLK).

Need to update the output?

digitalWrite (latchPin, LOW); // 10
SPI.transfer(newDataByte);
digitalWrite (latchPin, HIGH); // outputs update on this rising edge

Connect master reset pin to +5.
Connect OE pin to Gnd, or drive it with a PWM pin for brightness control

If you want LED animations to continue while other things are going on, you will have to completely restructure your program to use millis() for timing, instead of delay(). Also you will not be able to halt the program to wait for keypresses or for anything. Instead you have to use cooperative multitasking, shown in the IDE example "blinkwithoutDelay". The loop() function must run hundreds or thousands of times per second, in order to properly do "more than one thing at a time".

well thanks for answers ! :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.