BrainPad Arduino Code for Buzzer, Light- Temperature- and Accelerometer-Sensor

Reading the Light- and Temperature-Sensors was trivial.
For the Accelerometer there is an Arduino library at

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_SSD1306.h"

//http://www.arduinolibraries.info/libraries/mma8453_n0m1
#include <MMA8453_n0m1.h>


/* ============== MAIN =====================*/

//Use I2C with OLED RESET pin on D13
#define OLED_RESET 13
Adafruit_SSD1306 oled(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

uint32_t _BlueLED = PC6;
uint32_t _RedLED = PC9;
uint32_t _Button = PA5;

uint32_t buzzPin = PB8;
uint32_t LightSensor = PB1;
uint32_t TempSensor = PB0;
int reading;
char Buff[128];
int state = 0;

MMA8453_n0m1 accel;

void setup() {
    Serial.begin(9600);
    Serial.println("Hallo BrainPad");
    oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);   
    oled.display(); // show splashscreen
    delay(2000);
    oled.clearDisplay();
    oled.display();
    tone(buzzPin, 440, 500);
    delay(500);
    tone(buzzPin, 880, 500);  
    pinMode(_BlueLED, OUTPUT);    // Blue
    pinMode(_RedLED, OUTPUT);     // Red
    pinMode(_Button, INPUT_PULLUP);
    oled.setTextSize(1);
    oled.setTextColor(WHITE);
    accel.setI2CAddr(0x1C); //evtl. change your device address if necessary, default is 0x1C
    accel.dataMode(false, 2); //evtl. enable highRes 10bit, 2g range [2g,4g,8g]    
}
void loop() 
{ 
     // Beep if Button U pressed
     state = digitalRead(_Button);
    if (state == LOW)
    {
        tone(buzzPin, 880, 500);  
       delay(500);
    }
    // toggle blue LED  
    if (digitalRead(_BlueLED))
    { digitalWrite(_BlueLED, LOW); }
    else
    { digitalWrite(_BlueLED, HIGH); }

    oled.clearDisplay();
    oled.display();

   // Get and write light intensity
   reading = analogRead(LightSensor);
   int lightLevel = map(reading, 0, 1023, 0, 100);
   sprintf(Buff,"Light: %d", lightLevel);
   printLine(0, Buff);
   Serial.println(Buff);    

// Get and write temperature
reading = analogRead(TempSensor);
// The scaling is probably not correct
float tempLevel = (reading - 135) / 6.25;    
int prePoint = (int)(tempLevel / 1);
int postPoint = (int)(((tempLevel - prePoint) * 100) / 1); 
sprintf(Buff,"Temp: %d.%d", prePoint, postPoint);    
printLine(1, Buff);   
    
// Get and write Accelerometer values 
accel.update();
int accel_X = accel.x();
int accel_Y = accel.y();
int accel_Z = accel.z();
sprintf(Buff,"X: %d Y: %d Z: %d", accel_X, accel_Y, accel_Z);    
printLine(2, Buff);
oled.display();
delay(2000);       
}
void printLine(int line, const char* stringToWrite )
{  
   oled.setCursor(0, line * 8);
   oled.println(stringToWrite);
   oled.display();  
}
1 Like