Hi,
I wrote a very big sketch, i connected a lot of stuff to my UNO R4 WiFi because i want to learn everything ![]()
Now i connected a working, tested SHT21 board (i tested with the example provided, i also copied it to my sketch just to be sure while not changing any wiring but still doesn't succeed this way).
I couldn't make it print anything else to serial than Temp: -46.85 Humidity: -6.00 while the example gives me the correct Temp: 29.17 Humidity: 59.80 values.
#include "SPI.h"
//#include "IRremote.hpp"
#include "SHT21.h"
#include "Arduino_LED_Matrix.h"
#include "animation.h"
ArduinoLEDMatrix matrix;
SHT21 sht;
const int buttonPin = 7;
const int potmeterPin = A1;
const int lightSensorPin = A0;
const int rgbRPin = 3; //PWM
const int rgbGPin = 5; //PWM
const int rgbBPin = 6; //PWM
const int irSensorPin = 4;
const int greenLedPin = 2;
const int buzzerPin = 8;
const int redLedPin = 9;
const int slaveSelect = 12;
const int numberOfDigits = 4;
const int greenLedInterval = 500;
const int buzzerInterval = 100;
const int buttonInterval = 200;
const int LDRInterval = 100;
const int potInterval = 100;
const int pwmInterval = 5;
const int rgbFunInterval = 10;
const int displayRefreshInterval = 100;
const int irInterval = 5;
const int tempAndHumInterval = 100;
unsigned long currentMillis = 0;
unsigned long previousGreenLedMillis = 0;
unsigned long previousBuzzerMillis = 0;
unsigned long previousButtonMillis = 0;
unsigned long previousLDRMillis = 0;
unsigned long previousPotMillis = 0;
unsigned long previousPWMMillis = 0;
unsigned long previousRGBFun = 0;
unsigned long previousDisplayRefreshMillis = 0;
unsigned long previousIrMillis = 0;
unsigned long previousTempAndHumMillis = 0;
byte greenLedState = LOW;
byte buttonState = HIGH;
byte buzz = false;
int LDRValue;
int mappedLDRValue;
int potValue;
int mappedPotValue;
int pwmDuty;
int rgbRduty = 0;
int rgbGduty = 0;
int rgbBduty = 0;
byte rgbRUp = true;
byte rgbGUp = true;
byte rgbBUp = true;
float temp;
float humidity;
long randNumber;
//IRrecv irrecv(irSensorPin);
//decode_results results;
void setup() {
Wire.begin();
Serial.begin(9600);
SPI.begin();
//irrecv.enableIRIn();
pinMode(buttonPin, INPUT_PULLUP);
pinMode(potmeterPin, INPUT);
pinMode(lightSensorPin, INPUT);
pinMode(rgbRPin, OUTPUT);
pinMode(rgbGPin, OUTPUT);
pinMode(rgbBPin, OUTPUT);
pinMode(irSensorPin, INPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(slaveSelect, OUTPUT);
digitalWrite(slaveSelect, LOW);
sendCommand (12, 1); //normal mode (default is shutdown mode)
sendCommand (15, 0); //display test off
sendCommand (10, 0); //set light intensity (range is 0-15)
sendCommand (11, numberOfDigits); //7219 digit scan limit command
sendCommand (9, 255); //decode command, use standard 7-segment digits
digitalWrite(slaveSelect, HIGH);
randomSeed(analogRead(A5));
matrix.loadSequence(dinosaur);
matrix.begin();
matrix.play(true);
}
void loop() {
currentMillis = millis();
//greenLedBlinking();
buttonRead();
LDR();
potRead();
//irReceiving();
tempAndHum();
switchLED();
pwm(redLedPin);
//rgbFun();
soundBuzzer();
displayRefresh();
}
void greenLedBlinking() {
if (greenLedState == LOW) {
if(currentMillis - previousGreenLedMillis >= greenLedInterval) {
greenLedState = HIGH;
previousGreenLedMillis += greenLedInterval;
}
} else {
if(currentMillis - previousGreenLedMillis >= greenLedInterval) {
greenLedState = LOW;
previousGreenLedMillis += greenLedInterval;
}
}
}
void buttonRead() {
if (currentMillis - previousButtonMillis >= buttonInterval) {
if(digitalRead(buttonPin) == LOW) {
buttonState = !buttonState;
greenLedState = !greenLedState;
buzz = true;
}
previousButtonMillis += buttonInterval;
}
}
void LDR() {
if(currentMillis - previousLDRMillis >= LDRInterval) {
LDRValue = analogRead(lightSensorPin);
mappedLDRValue = map(LDRValue, 1023, 0, 0, 15);
previousLDRMillis += LDRInterval;
}
}
void potRead() {
if(currentMillis - previousPotMillis >= potInterval) {
potValue = analogRead(potmeterPin);
pwmDuty = potValue / 4;
mappedPotValue = map(potValue, 0, 1023, 0, 100);
previousPotMillis += potInterval;
}
}
void switchLED() {
digitalWrite(greenLedPin, greenLedState);
}
void pwm(int pwmPin) {
if(currentMillis - previousPWMMillis >= pwmInterval) {
analogWrite(pwmPin, pwmDuty);
previousPWMMillis += pwmInterval;
}
}
void rgbFun() {
if(currentMillis - previousRGBFun >= rgbFunInterval) {
randNumber = random(1, 4);
switch(randNumber) {
case 1:
if(rgbRUp) {
rgbRduty++;
analogWrite(rgbRPin, rgbRduty);
if(rgbRduty >= 255) rgbRUp = false;
} else {
rgbRduty--;
analogWrite(rgbRPin, rgbRduty);
if(rgbRduty <= 0) rgbRUp = true;
}
break;
case 2:
if(rgbGUp) {
rgbGduty++;
analogWrite(rgbGPin, rgbGduty);
if(rgbGduty >= 255) rgbGUp = false;
} else {
rgbGduty--;
analogWrite(rgbGPin, rgbGduty);
if(rgbGduty <= 0) rgbGUp = true;
}
break;
case 3:
if(rgbBUp) {
rgbBduty++;
analogWrite(rgbBPin, rgbBduty);
if(rgbBduty >= 255) rgbBUp = false;
} else {
rgbBduty--;
analogWrite(rgbBPin, rgbBduty);
if(rgbBduty <= 0) rgbBUp = true;
}
break;
}
previousRGBFun += rgbFunInterval;
}
}
void soundBuzzer() {
if(currentMillis - previousBuzzerMillis >= buzzerInterval) {
if(buzz == true) {
tone(buzzerPin, 1000);
buzz = false;
} else {
noTone(buzzerPin);
}
previousBuzzerMillis += buzzerInterval;
}
}
void displayRefresh() {
if(currentMillis - previousDisplayRefreshMillis >= displayRefreshInterval) {
//sendCommand (10, mappedLDRValue); //set light intensity (range is 0-15)
displayNumber(mappedPotValue);
previousDisplayRefreshMillis += displayRefreshInterval;
}
}
void displayNumber (int number) {
for (int i=0; i < numberOfDigits; i++) {
byte character= number % 10; //get the value of the rightmost digit
if (number == 0 && i > 0) {
character = 0xf;
}
sendCommand(numberOfDigits-i, character);
number= number/10;
}
}
void sendCommand(int command, int value) {
digitalWrite(slaveSelect, LOW);
SPI.transfer(command);
SPI.transfer(value);
digitalWrite(slaveSelect, HIGH);
}
/*
void irReceiving() {
if(currentMillis - previousIrMillis >= irInterval) {
if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);
dump(&results);
irrecv.resume(); // Receive the next value
}
previousIrMillis += irInterval;
}
}
//void dump(decode_results *results) {
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
int count = results->rawlen;
if (results->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY) {
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) {
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) {
Serial.print("Decoded RC6: ");
}
else if (results->decode_type == PANASONIC) {
Serial.print("Decoded PANASONIC - Address: ");
Serial.print(results->address, HEX);
Serial.print(" Value: ");
}
else if (results->decode_type == LG) {
Serial.print("Decoded LG: ");
}
else if (results->decode_type == JVC) {
Serial.print("Decoded JVC: ");
}
else if (results->decode_type == AIWA_RC_T501) {
Serial.print("Decoded AIWA RC T501: ");
}
else if (results->decode_type == WHYNTER) {
Serial.print("Decoded Whynter: ");
}
Serial.print(results->value, HEX);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 1; i < count; i++) {
if (i & 1) {
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else {
Serial.write('-');
Serial.print((unsigned long) results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println();
}
*/
void tempAndHum() {
if(currentMillis - previousTempAndHumMillis >= tempAndHumInterval) {
temp = sht.getTemperature();
humidity = sht.getHumidity();
Serial.print("Temp: ");
Serial.print(temp);
Serial.print("\t Humidity: ");
Serial.println(humidity);
previousTempAndHumMillis += tempAndHumInterval;
}
}
The function is at the very bottom, i have no electrical shorts and the example code below works just fine.
//==============================================================================
// E - R A D I O N I C A . C O M, H.Kolomana 6/A, Djakovo 31400, Croatia
//==============================================================================
// Project : SHT21 Arduino Library
// File : SHT21 Demo Arduino Example
// Author : e-radionica.com 2017
// Licence : Open-source !
//==============================================================================
//==============================================================================
// Use with any SHT21 breakout. Check ours:
// https://e-radionica.com/en/sht21-humidity-and-temperature-sensor.html
// Shows temperature and humidity from SHT21 sensor in Serial monitor.
// If any questions,
// just contact techsupport@e-radionica.com
//==============================================================================
#include <SHT21.h> // include SHT21 library
SHT21 sht;
float temp; // variable to store temperature
float humidity; // variable to store hemidity
void setup() {
Wire.begin(); // begin Wire(I2C)
Serial.begin(9600); // begin Serial
}
void loop() {
temp = sht.getTemperature(); // get temp from SHT
humidity = sht.getHumidity(); // get temp from SHT
Serial.print("Temp: "); // print readings
Serial.print(temp);
Serial.print("\t Humidity: ");
Serial.println(humidity);
delay(85); // min delay for 14bit temp reading is 85ms
}
Now i also found out that everything went super laggy while i got the wrong values on the Serial. I mean the connected potmeter, button and 7 segment display all went to like 1fps, i need to hold down a lot the button for it to register, the buzzer buzzes longer than it should etc..
Almost if i used some delay() every time i access the sensor.
Can i get a little help? ![]()