Hello guys,
I have a problem with my Arduino code. I'm trying to generate a random number between 0 and 6, but it seems that the random
function always returns the number 6. Here is the relevant code excerpt:
number = random(0, 6);
To briefly explain what it's all about, I'm building/coding a digital dice that is activated when you move the mpu6050.
How can i fix it?
Code:
#include "I2Cdev.h"
#include <arduino.h>
#include "MPU6050.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
int16_t ax, ay, az;
int16_t gx, gy, gz;
#define OUTPUT_READABLE_ACCELGYRO
#define LED_PIN 13
bool blinkState = false;
#define buzzer 12
MPU6050 mpu;
const int Empfindlichkeit = 100;
int segmentPins[] = {8, 9, 3, 4, 5, 7, 6};
bool motion = true;
int number = 1;
void setup() {
for (int i = 0; i < 8; i++){
pinMode(segmentPins[i], OUTPUT);
}
pinMode(buzzer, OUTPUT);
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
Serial.begin(38400);
Serial.println("Initializing I2C devices...");
mpu.initialize();
Serial.println("Testing device connections...");
Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
pinMode(LED_PIN, OUTPUT);
displayNumber(1);
}
void loop() {
motion_detaction();
if (motion == true){
sevseg_disliay();
}
}
void motion_detaction(){
pinMode(buzzer, OUTPUT);
// Lese die Beschleunigungsdaten
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
// Berechne die Gesamtbeschleunigung
int16_t totalAcceleration = abs(ax) + abs(ay) + abs(az);
// Überprüfe, ob die Gesamtbeschleunigung den Schwellenwert überschreitet
if (totalAcceleration > Empfindlichkeit) {
motion = false;
} else {
motion = true;
}
// Ausgabe der Daten
Serial.print("Beschleunigung: ");
Serial.print("X = "); Serial.print(ax);
Serial.print(" | Y = "); Serial.print(ay);
Serial.print(" | Z = "); Serial.print(az);
Serial.print(" | Gesamt = "); Serial.print(totalAcceleration);
Serial.print(" | Bewegung: "); Serial.println(motion);
delay(100); // Wartezeit für die Stabilität der Ausgabe
}
void sevseg_disliay(){
if (motion == true) {
number = random(0, 6);
for (int i = 0; i < 5; i++) {
displayNumber(1);
delay(20);
displayNumber(2);
delay(20);
displayNumber(3);
delay(20);
displayNumber(4);
delay(20);
displayNumber(5);
delay(20);
displayNumber(6);
delay(20);
tone(buzzer, 800, 50);
}
}
else if (motion == false) {
switch (number)
{
case 1: displayNumber(1);
break;
case 2: displayNumber(2);
break;
case 3: displayNumber(3);
break;
case 4: displayNumber(4);
break;
case 5: displayNumber(5);
break;
case 6: displayNumber(6);
break;
}
delay(50);
}
}
void displayNumber(int num) {
// Definition der Segmente für die Zahlen 0 bis 9
// {A, B, C, D, E, F, G, DP}
byte numbers[][7] = {
{0, 1, 1, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1}, // 2
{1, 1, 1, 1, 0, 0, 1}, // 3
{0, 1, 1, 0, 0, 1, 1}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 0, 1, 1, 1, 1, 1}
};
// Übertrage die Segmente für die gewünschte Zahl auf die Pins
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], numbers[num - 1][i]);
}
}