I am using Arduino nano ble 33, and I want to covert accelerometer values which are x,y,z into frequencies then these frequencies values need to be sent through the Arduino nano 33 ble. Can someone help me with this issue? Thanks in advance.
Here is my code: The code now only reads the raw data from the accelerometer.
< #include "Arduino.h"
#include <ArduinoBLE.h>
#include "Nano33BLEAccelerometer.h"
// #include "arduinoFFT.h"
#define BLE_BUFFER_SIZES 20
#define BLE_DEVICE_NAME "Nano 33 BLE"
#define BLE_LOCAL_NAME "Accelerometer BLE"
Nano33BLEAccelerometerData accelerometerData;
BLEService BLEAccelerometer("590d65c7-3a0a-4023-a05a-6aaf2f22441c");
BLECharacteristic accelerometerXBLE("0004", BLERead | BLENotify | BLEBroadcast,
BLE_BUFFER_SIZES);
BLECharacteristic accelerometerYBLE("0005", BLERead | BLENotify | BLEBroadcast, BLE_BUFFER_SIZES);
BLECharacteristic accelerometerZBLE("0006", BLERead | BLENotify | BLEBroadcast, BLE_BUFFER_SIZES);
char bleBuffer[BLE_BUFFER_SIZES];
void setup()
{
Serial.begin(115200);
while(!Serial);
if (!BLE.begin())
{
while (1);
}
else
{
BLE.setDeviceName(BLE_DEVICE_NAME);
BLE.setLocalName(BLE_LOCAL_NAME);
BLE.setAdvertisedService(BLEAccelerometer);
BLEAccelerometer.addCharacteristic(accelerometerXBLE);
BLEAccelerometer.addCharacteristic(accelerometerYBLE);
BLEAccelerometer.addCharacteristic(accelerometerZBLE);
BLE.addService(BLEAccelerometer);
BLE.advertise();
Accelerometer.begin();
accelerometerXBLE.writeValue((byte)0x01);
accelerometerYBLE.writeValue((byte)0x01);
accelerometerZBLE.writeValue((byte)0x01);
Serial.println("X, Y, Z");
}
}
void loop()
{
BLEDevice central = BLE.central();
if(central)
{
Serial.println("Connected");
int writeLength;
while(central.connected())
{
if(Accelerometer.pop(accelerometerData))
{
writeLength = sprintf(bleBuffer, "%f", accelerometerData.x);
accelerometerXBLE.writeValue(bleBuffer);
writeLength = sprintf(bleBuffer, "%f", accelerometerData.y);
accelerometerYBLE.writeValue(bleBuffer);
writeLength = sprintf(bleBuffer, "%f", accelerometerData.z);
accelerometerZBLE.writeValue(bleBuffer);
delay(1000);
Serial.printf("%f,%f,%f\r\n", accelerometerData.x, accelerometerData.y,
accelerometerData.z);
}
}
}
} >
What you are asking is not simple. You will need to use a Fourier transformation. Read about it here: https://see.stanford.edu/materials/lsoftaee261/chap8.pdf
Related
I am trying to make the PN532 NFC reader module and mcp2515 to work together.
This is my code:
#include <SPI.h>
#include <mcp2515.h> //Library for using CAN Communication
#include <PN532_SPI.h>
#include <PN532.h>
#include <NfcAdapter.h>
#define deviceAddress 0x42
struct can_frame canMsg;
MCP2515 mcp2515(10);
PN532_SPI pn532spi1(SPI, 5);
PN532_SPI pn532spi2(SPI, 6);
NfcAdapter nfc1 = NfcAdapter(pn532spi1);
NfcAdapter nfc2 = NfcAdapter(pn532spi2);
void setup(void) {
Serial.begin(9600);
Serial.println("Starting...");
mcp2515.reset();
mcp2515.setBitrate(CAN_10KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
delay(100);
nfc1.begin();
delay(100);
nfc2.begin();
}
void sendMessage(int data) {
canMsg.can_id = deviceAddress;
canMsg.can_dlc = 1;
canMsg.data[0] = data;
mcp2515.sendMessage(&canMsg);
}
void loop() {
if (nfc1.tagPresent())
{
Serial.println("Kattenhoofd");
NfcTag tag1 = nfc1.read();
if (tag1.getUidString() == "6C 1A D6 B9") {
Serial.println("YASSS!!!");
}
}
if (nfc2.tagPresent())
{
Serial.println("Konijnenvoet");
NfcTag tag2 = nfc2.read();
if (tag2.getUidString() == "59 07 CA 5C") {
Serial.println("YASSS!!!");
}
}
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
Serial.println("RECEIVED");
}
}
When I try this the two NFC readers only read the value once and then stop reading the tag.
Is there something wrong with my code? All the wiring is done correctly.
When I remove the mcp2515 and it initializations the nfc readers work as expected.
I have a temperature sensor set up working with an LCD and a stick to adjust the brightness. I now want the temperature sensor to send a text whenever it reaches a certain temperature. Can somebody please help.
The GSM unit i have is the SIM800L
below is what I have so far :
#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int sensor=A1; // Assigning Analog Pin A1 to variable 'sensor'
float tempc; //variable to store temperature in degree Celsius
float tempf; //variable to store temperature in Fahrenheit
float vout; //temporary variable to hold sensor reading
void setup()
{
pinMode(sensor,INPUT); // Configuring pin A1 as INPUT Pin
Serial.begin(9600);
lcd.begin(16,2);
delay(500);
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
}
you could use the library Fona to send SMS with a Sim800L
to send a message you use the command -> fona.sendSMS(sendto, message)
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
while (!Serial);
Serial.begin(115200);
Serial.println(F("FONA basic test"));
Serial.println(F("Initializing....(May take 3 seconds)"));
fonaSS.begin(9600);
if (!fona.begin(fonaSS)) {
Serial.println(F("Couldn't find FONA"));
while (1);
}
Serial.println(F("FONA is OK"));
char sendto[21], message[141];
:
:
//initialize sendto and message
:
:
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
to adapt the program to your case: i have put some lines of codes from setup to loop (easy to understant sendto and message definitions in loop now)
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
fonaSS.begin(9600);
// you initialisation code
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
if (tempc > 30.0) {
SendSms();
}
}
void SendSms() {
char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
char message[141];
sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
//sends the message via SMS
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
another way to send sms
you could test the hayes command: for example
void sendsms(){
Serial.println("Sending text message...");
fonaSS.print("AT+CMGF=1\r"); // SMS MODE
delay(100);
// phone number
fonaSS.print("AT+CMGS=\"+33676171212\"\r"); //indicate your phone number
delay(100);
// message here
fonaSS.print("Message test \r");
// CTR+Z in mode ASCII, to indicate the end of message
fonaSS.print(char(26));
delay(100);
fonaSS.println();
Serial.println("Text send");
}
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
// you initialisation code
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
if (tempc > 30.0) {
SendSms();
}
}
void SendSms() {
char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
char message[141];
sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
//sends the message via SMS
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
I am currently developing a driver for the RM3100 magnetometer but am running into issues reading and writing to the registers. Currently, I am trying to read the STATUS register to determine if the sensor is collecting data; however, I get a zero every time. Is there an issue with the way that I am trying to access the registers? Or is there some error in my code?
C++ file:
#include "Arduino.h"
#include "QRM3100.h"
#include <Wire.h>
#include <math.h>
#define CALIBRATION_TIMEOUT 5000 //timeout in milliseconds
#define DEG_PER_RAD (180.0/3.14159265358979)
QRM3100::QRM3100() {
//Just some random initial values
}
void QRM3100::initialize() {
Wire.begin();
}
uint8_t QRM3100::readRegister(uint8_t address){
uint8_t output;
Wire.beginTransmission(QRM3100_I2C_ADDRESS);
Wire.write(address);
Wire.endTransmission();
delayMicroseconds(20);
Wire.requestFrom(QRM3100_I2C_ADDRESS, 1);
while(Wire.available())
{
output = Wire.read();
}
return output;
}
void QRM3100::writeRegister(uint8_t address, uint8_t value){
Wire.beginTransmission(QRM3100_I2C_ADDRESS);
Wire.write(address);
Wire.write(value);
Wire.endTransmission();
}
bool QRM3100::dataReady() {
return ((readRegister(QRM3100_STATUS_REG) & 0x80) >> 7);
}
void QRM3100::start() {
exitStandby();
}
void QRM3100::exitStandby(){
writeRegister(QRM3100_CMM, 0b011100001);
}
Header file:
#ifndef QRM3100_h
#define QRM3100_h
#include "Arduino.h"
#include "Wire.h"
#define QRM3100_CMM 0x01 //initiates continuous measurement mode
#define QRM3100_STATUS_REG 0x34 //status of DRDY
#define QRM3100_I2C_ADDRESS 0x21
#define QRM3100_POLL 0x00 //poll
class QRM3100
{
public:
QRM3100();
void initialize();
uint8_t readRegister(uint8_t address);
void writeRegister(uint8_t address, uint8_t value);
bool dataReady();
void start();
void exitStandby();
bool error;
};
#endif
Arduino file:
#include <C:\Users\daehy\OneDrive\Documents\Arduino\libraries\src\QRM3100X.h>
#include <C:\Users\daehy\OneDrive\Documents\Arduino\libraries\src\QRM3100X.cpp>
QRM3100 mag = QRM3100();
void setup() {
Serial.begin(9600);
mag.initialize();
mag.start();
}
void loop() {
Serial.println( mag.dataReady());
}
It seems you haven't set the CMM register first. The default value may not be correct for continuous mode. Try setting the CMM register (0x01) to the following:
(CMM_ALL_AXIS_ON | DRDY_WHEN_ALL_AXIS_MEASURED | CM_START)
( 0x70 | 0x08 | 0x01 )
that should give you a final binary value of,
|LDM|CMZ|CMY|CMX|DRDM1|DRDM0|ALARM|START|
| 0 | 1 | 1 | 1 | 1 | 0 | 0 | 1 |
or 0x79 in hex
I'm new to this site and also the wonderful world of Arduino, I have been playing around with a Leonardo board and some Neopixel LEDS ( WS2812B ). I'm currently trying to set predefined colors on the LEDs with a single Pot, but also have an interrupt for a PIR sensor. I'm using the Fastled library since its great but at this point I seem to be stuck on the interrupt part. Any guidance will be appreciated !
#include "FastLED.h"
#define NUM_LEDS 3
#define DATA_PIN 6
#define PIR_PIN 2
int PIR_PIN = 0
// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
CRGB leds[NUM_LEDS];
void setup() {
// initialize the LED pin as an output:
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
// initialize serial communications:
pinMode(PIR_PIN, INPUT);
attachInterrupt(0, PIR_PIN, CHANGE);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
if (analogPin < 1000)
{
leds[0] = CRGB::Red;
FastLED.show();// do Thing A
}
else if (analogPin < 500)
{
leds[0] = CRGB::Orange;
FastLED.show();// do Thing B
}
else if (analogPin < 250)
{
leds[0] = CRGB::Green;
FastLED.show();//do fuckity
}
else
{
leds[0] = CRGB::Purple;
FastLED.show();// do Thing C
}
}
void PIN_PIR () {
buttonState = digitalRead(PIR_PIN);
digitalWrite(DATA_PIN, buttonState);
}
I'm trying to request temperatures from my DS18B20 sensor to post on plot.ly, but it seems my loop function is only running once; after connecting to plot.ly and creating the graph, the temperature is printed once in the serial monitor and does not seem to continue! Any help is greatly appreciated. Here is my code:
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <plotly_streaming_cc3000.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define WLAN_SSID "wifi"
#define WLAN_PASS "********"
#define WLAN_SECURITY WLAN_SEC_WPA2
OneWire oneWire(10);
DallasTemperature sensors(&oneWire);
#define nTraces 1
char *tokens[nTraces] = {"token"};
plotly graph("username", "token", tokens, "filename", nTraces);
void wifi_connect(){
/* Initialise the module */
Serial.println(F("\n... Initializing..."));
if (!graph.cc3000.begin())
{
Serial.println(F("... Couldn't begin()! Check your wiring?"));
while(1);
}
// Optional SSID scan
// listSSIDResults();
if (!graph.cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("... Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("... Request DHCP"));
while (!graph.cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
unsigned long aucDHCP = 14400;
unsigned long aucARP = 3600;
unsigned long aucKeepalive = 10;
unsigned long aucInactivity = 20;
if (netapp_timeout_values(&aucDHCP, &aucARP, &aucKeepalive, &aucInactivity) != 0) {
Serial.println("Error setting inactivity timeout!");
}
}
}
void setup() {
graph.maxpoints = 100;
Serial.begin(9600);
sensors.begin();
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
wifi_connect();
bool success;
success = graph.init();
if(!success){while(true){}}
graph.openStream();
}
void loop(void) {
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
Serial.print("Temperature for Device 1 is: ");
Serial.print(sensors.getTempFByIndex(0));
graph.plot(millis(), sensors.getTempFByIndex(0), tokens[0]);
delay(500);
}