I'm making a program where a buzzer activates if the accelerometer is tilted a certain number of degrees. I'm getting an error that says "'I2C_MODE' was not declared in this scope." I'm using a Grove Beginner Kit, so all of the parts are automatically connected to each other. I downloaded Seeed_Arduino_LIS3DHTR library from the following link: https://github.com/Seeed-Studio/Seeed_Arduino_LIS3DHTR and used sample code from the Grove Beginner Kit for Arduino Guide that came with the board, so everything should work properly. I'm getting this error and don't want to move further with the project until I figure out what's causing this error.
#include <LIS3DHTR.h>
//Gravity Acceleration
#include "LIS3DHTR.h"
#ifdef SOFTWAREWIRE
#include <SoftwareWire.h>
SoftwareWire myWire(3, 2);
LIS3DHTR<SoftwareWire> LIS(I2C_MODE); //IIC This is what the error
#define WIRE myWire
#else
#include <Wire.h>
LIS3DHTR<TwoWire> LIS(I2C_MODE);//IIC THIS IS WHERE THE ERROR OCCURS
#define WIRE Wire
#endif
void setup() {
Serial.begin(9600);
while (!Serial) {};
LIS.begin(WIRE); //IIC init
delay(100);
LIS.setOutputDataRate(LIS3DHTR_DATARATE_50HZ);
}
void loop() {
if (!LIS) {
Serial.println("LIS3DHTR didn't connect.");
while (1);
return;
}
//3 axis
Serial.print("x:"); Serial.print(LIS.getAccelerationX()); Serial.prin
t(" ");
Serial.print("y:"); Serial.print(LIS.getAccelerationY()); Serial.prin
t(" ");
Serial.print("z:"); Serial.println(LIS.getAccelerationZ());
delay(500);
}
The docs you linked have a different instantiation than what you have!
Try passing Wire to the .begin method rather than to LIS (which presumably does exist in Wire.h and likely also exists in SoftwareWire.h)
Plausibly putting LIS.begin() in setup() is needed, though it doesn't seem to matter from their examples
LIS3DHTR<TwoWire> LIS; //IIC
LIS.begin(Wire, 0x19)
Related
I am writing a wrapper for neopixel library. I am adding a full source code of my program:
I have created my own custom function toggle_led_strip which is using a function from the neopixelbus led strip library.
#include "NeoPixelBus.h"
#include <Adafruit_I2CDevice.h>
#define PixelCount 8 // this example assumes 4 pixels, making it smaller will cause a failure
#define PixelPin 27 // make sure to set this to the correct pin, ignored for Esp8266
#define colorSaturation 250
RgbColor blue(0,0,255);
RgbColor black(0);
NeoPixelBus<NeoGrbFeature, NeoEsp32Rmt0800KbpsMethod> strip(PixelCount, PixelPin);
void toggle_led_strip(RgbColor colour){
strip.SetPixelColor(0, colour);
strip.Show();
delay(100);
strip.SetPixelColor(0, black);
strip.Show();
delay(100);
}
void setup() {
strip.Begin();
// put your setup code here, to run once:
}
void loop() {
toggle_led_strip(blue);
// put your main code here, to run repeatedly:
}
Normally, when I want to create a colour variable, I must create it in such way:
RgbColor blue(0,0,255);
RgbColor black(0);
However, I am learning about different ways of creating colour objects and someone has suggested me to use ENUM and array method as such:
enum COLORS
{
blue,black
};
RgbColor a_color[2] = {
[blue] = {0,0,255},
[black] ={0}
};
From what I understand, the code above will set the first variable of enum (blue) to the {0,0,255} and the second variable of enum (black) to {0} so the result should be exactly the same as if I have used
RgbColor blue(0,0,255);
RgbColor black(0);
is that correct understanding?
Then I try to pass the color to the function as following:
//void toggle_led_strip(RgbColor colour) This is function prototype
toggle_led_strip(blue)
But it does not work when using enum and array method and works perfectly with the first method
Those who are interested in the solution:
void toggle_led_strip(COLORS colour){
strip.SetPixelColor(0, a_color[colour]);
strip.Show();
delay(100);
strip.SetPixelColor(0, a_color[black]);
strip.Show();
delay(100);
}
It turned out to be quite simple once you understand that an ENUM is treated as an array index and nothing else. It works as expected now.
I am currently working on a project. I tried different sample testing code to send sms to my current number but it won't sent any.
Should my SIM 800L GSM module sim and my phone number have the same carrier?
I even followed the other people's advices on using a 3.7v power supply.
Library already included.
Correct RX-TX TX-RX, VCC, RST pins.
Below is a sample code from what I have been following.
#include <Sim800l.h>
#include <SoftwareSerial.h> //is necesary for the library!!
Sim800l Sim800l; //to declare the library
char* text;
char* number;
bool error; //to catch the response of sendSms
void setup(){
Sim800l.begin(); // initializate the library.
text="Testing Sms"; //text for the message.
number="2926451386"; //change to a valid number.
error=Sim800l.sendSms(number,text);
// OR
//Sim800l.sendSms("+540111111111","the text go here")
}
void loop(){
//do nothing
}
This should be sending a text message to a phone number but I am not receiving any.
I am using STM32F407 Discovery Board for interfacing USB OTG FS. I am using CubeMx and Keil for development.
First thing first, I have enabled PC0 - USB_Power(for Discovery Board) and the state is RESET for proper USB running.I have enabled PA9 - VBUS as GPIO Input.My System is running at 168MHz.Have used MAX_SS(Max Sector size) - 4096(This option is available in Cube Mx).Enabled USB as Host and used FATFS provided by CubeMX.Enbaled MSC(Mass Storage Class).
CODE:
#include "main.h"
#include "stm32f4xx_hal.h"
#include "fatfs.h"
#include "usb_host.h"
#define GREEN_High HAL_GPIO_WritePin(GREEN_GPIO_Port,GREEN_Pin,GPIO_PIN_SET)
#define GREEN_Low HAL_GPIO_WritePin(GREEN_GPIO_Port,GREEN_Pin,GPIO_PIN_RESET)
#define ORANGE_High HAL_GPIO_WritePin(ORANGE_GPIO_Port,ORANGE_Pin,GPIO_PIN_SET)
#define ORANGE_Low HAL_GPIO_WritePin(ORANGE_GPIO_Port,ORANGE_Pin,GPIO_PIN_RESET)
extern USBH_HandleTypeDef hUsbHostFS;
extern ApplicationTypeDef Appli_state;
FATFS USBDISKFatFs;
FIL MyFile;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void MX_USB_HOST_Process(void);
void Green_Blink(uint16_t ms);
void Orange_Blink(uint16_t ms);
void USB_Write_Demo(char* fileName);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USB_HOST_Init();
MX_FATFS_Init();
Green_Blink(100);
Orange_Blink(100);
while (1)
{
MX_USB_HOST_Process();
if (Appli_state == APPLICATION_START)
{
USB_Write_Demo("myCSV.csv");
}
*THIS IS THE AREA OF PROBLEM*
// else if (Appli_state == APPLICATION_IDLE)
// {
// GREEN_High;
// ORANGE_High;
// HAL_Delay(100);
// GREEN_Low;
// ORANGE_Low;
// HAL_Delay(100);
// }
}
}
void USB_Write_Demo(char *fileName)
{
FRESULT fres;
uint32_t bytesWritten;
uint8_t w_text[] = {"Hello, I, AM, STM32, Discovery\r\n"};
if (f_mount(&USBDISKFatFs,(TCHAR const*)USBHPath,0) != FR_OK)
{
Orange_Blink(1000);
Error_Handler();
}
else
{
Green_Blink(100);
if (open_append(&MyFile,fileName) != FR_OK)
{
Orange_Blink(100);
Error_Handler();
}
else
{
Green_Blink(100);
fres = f_write(&MyFile,w_text,sizeof(w_text),(void*)bytesWritten);
if (bytesWritten == 0 || fres != FR_OK)
{
Orange_Blink(100);
Error_Handler();
}
else
{
f_close(&MyFile);
Green_Blink(100);
}
}
}
}
void Green_Blink(uint16_t ms)
{
GREEN_High;
HAL_Delay(ms);
GREEN_Low;
HAL_Delay(ms);
}
void Orange_Blink(uint16_t ms)
{
ORANGE_High;
HAL_Delay(ms);
ORANGE_Low;
HAL_Delay(ms);
}
So what is happening here is i am creating a CSV file and with every loop i am appending the new data in it. And i am really succesffull in doing so. I have created a really long(500KB not so long) csv file using this particular code.
But i have found an abnormality here which i am not able to understand.
When i add this part to the code, there is no file created and every iteration the control reaches this function.
else if (Appli_state == APPLICATION_IDLE)
{
GREEN_High;
ORANGE_High;
HAL_Delay(100);
GREEN_Low;
ORANGE_Low;
HAL_Delay(100);
}
I am not able to understand how this function is affecting the working code. I am sure that APPLICATION_START and APPLICATION_IDLE are two diffrent things. When i comment this portion of the code, Everything is just fine i can make files as long as my storage doesn't end.
It took me several hours(like 2days) to figure out this is the problem.
I tried to increase Minimum heap size - 0x2000 and Minimum Stack size - 0x4000 (This option is available on linker setting in cubeMx. While generating file the place where you give the project name,location and all there only)
Any suggestions will be helpful as i am out of ideas.
I found a way to deal with this problem without using RTOS. As i have never tried RTOS before It was difficult to complete the project in few days.
The idea is simple. It is we need to wait till MX_USB_HOST_Process() doesn't returns Appli_state as Idle.
I dont take the credit.
You can check this LINK
so i added a new function in usb_host.c which retuns Appli_state
uint8_t IsUSB_Busy(void)
{
return Appli_state;
}
And in main.c i waited till it returns anything but 0. As APPLICATION_IDLE=0
typedef enum {
APPLICATION_IDLE = 0,
APPLICATION_START,
APPLICATION_READY,
APPLICATION_DISCONNECT
}ApplicationTypeDef;
Added this bit of code in main file and everything is working as expected
while (!IsUSB_Busy())
{
MX_USB_HOST_Process();
}
I hope someone finds it helpful.
And Thanks for your help.
You can create two Tasks (FreeRTOS) with CubeMX, separate the USB stuff from the LED stuff. taskLED() and taskUSB()
So we connected a sound sensor to our board to light up our LED light when sound is heard, it kinda works but there some hiccups.
We tried messing with the code for a while but no matter what we do the senor will only react to loud even when we put in a threshold. If you see in the picture, it it only displaying "loud" noise to the display and cant seem be able to go to the other condition we set in our threshold. We configure the sensor with our screw driver but nothing seem to work. Our code is below & before we continue on, we wanted to know if there a problem with it that can fix out the issue,thanks you
ALSO the sound sensor is a "ko9A01"
PS: we use "energia" to code this.
#include <msp430.h>
#include <Wire.h>
int soundsensor = 2;
int led = 3;
void setup()
{
Serial.begin(9600);
Serial.println("Begin Test");
pinMode(soundsensor,OUTPUT);
pinMode(led,OUTPUT);
}
void loop()
{
int sensorValue = digitalRead(soundsensor);
Serial.println(sensorValue);
delay(250);
if (sensorValue == 1)
{
Serial.print("LOUD");
digitalWrite(led,HIGH);
}
else
{
Serial.print("QUIET");
digitalWrite(led,LOW);
}
}
EDIT: NOW With the help of Brydon we change the output to input and change it to this we change it to this and now we get this new error voi
void setup()
{
Serial.begin(9600);
Serial.println("Begin Testing");
pinMode(soundsensor,INPUT);
}
and it only show
"begin test":
0 and wont move from there
You have the sound sensor configured as an OUTPUT in the setup.
I assume you want it to be an input? That would be the case if you're reading values from it.
I can't tell what sensor you have - but with more information on the sensor, we can read the documentation and help you configure the inputs appropriately (ie a threshold)
I would like to use the timer_us function of the NewPing library in order to run a function every second without blocking. My minimal example looks like this:
// setup timer
#include <NewPing.h>
timer_us(1000, sensoring);
void setup() {
Serial.begin(19200)
}
void loop() {}
void sensoring() {
Serial.print("ok, it's working")
}
But it is not compiling because of:
expected constructor, destructor, or type conversion before '(' token
My hardware is:
Arduino: 1.8.3 (Mac OS X), Board: "Arduino Nano, ATmega328"
Two mistakes:
The syntax is NewPing::timer_ms(.
Put that line inside setup().