Sound Sensor RGB LED coding - led

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)

Related

Arduino error: 'I2C_MODE' was not declared in this scope

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)

debounce with two button to turn on different leds with Arduino

I want to make debounce for two buttons. So turn on Red led or Green led knowing which button have pressed My program only works for 1 button.
I think that I need a array for two several buttons.
Could anyone help me to improve my code two buttons or more ?
int boton = 11;
int led = 13;
boolean estadoAnterior = LOW;
boolean estadoActual = LOW;
boolean ledOn = false;
int cont=0;
void setup()
{
pinMode(boton, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
}
boolean rebote(boolean eAnterior)
{
boolean eActual = digitalRead(boton);
if (eAnterior != eActual)
{
delay(5);
eActual = digitalRead(boton);
}
return eActual;
}
void loop()
{
estadoActual = rebote(estadoAnterior);
if (estadoAnterior == LOW && estadoActual == HIGH)
{
ledOn = !ledOn;
cont++;
Serial.println(cont);
}
estadoAnterior = estadoActual;
digitalWrite(led, ledOn);
}
Overall, using a busy-delay is a rather crude way to de-bounce buttons, but it will work for hobbyist purposes.
Change estadoAnterior into an array of expected button states.
Make an array of ints for the button pins boton. (In real MCU programming, this would be an array of ports + masks.)
In the debounce function, make an array of de-bounce bool values for buttons that need debouncing. Check which ones that need debouncing in a loop.
If any button needed debouncing, wait 5ms. Not per button, but once. (Btw 5ms may or may not be enough depending on button type)
Read that button again and use the result.
A more professional approach might be something along the lines of this.
Once done, rewrite all identifiers to English. We don't use our native language when programming, because that turns the source code into a mess. Your code is some mix of English and your native language, it is very hard to read.

serial.println(String) prints "#" instead of the string

i'm just trying to recieve a string using a serial and to send this string back. So when i send a string to an arduino over a serial the arduino should automaticly send this string back.
i created this code:
String test;
void setup(){
Serial.begin(9600);
Serial.setTimeout(2);
test = "null";
}
void loop(){
if(Serial.available()){
test = Serial.readString();
}
Serial.println(test);
}
I guess it is not that difficult to understand. However now the arduino will always print a "#" instead of the variable test. My connected serial device is a bluetooth modul. (hc-06)
What did i do wrong?
Thank you!
(i also ran this code in the arduino emulator 123D Circuits. There it worked just fine)
You need change your code. Move println into if statement.
Try increase timeout interval, 2ms is not enough, good value (at 9600) lies above 10ms. Theoretically timeout should be at least 3.5 characters long and for current speed this equals ~0,4 ms. But in practice higher values are used.
String test;
void setup(){
Serial.begin(9600);
Serial.setTimeout(10);// or more
test = "null";
}
void loop(){
if(Serial.available()){
test = Serial.readString();
Serial.println(test);// moved into if
}
}
Update: Another simple solution to return characters back looks like:
void loop(){
if(Serial.available()) Serial.write(Serial.read());
}
Update 2: Had similar issue with BLE module HM10 (clone, not official). It was sending about 15 dummy bytes prior to any array. And i didn't solve it. But if weired bytes always the same you can make a simple trick using String.remove():
if(Serial.available()){
test = Serial.readString();
test.remove(0,5);
// test.remove - add code to remove last character
Serial.println(test);
}
Also try another terminal.

Problems with Arduino remotely controlling an outlet

This is a project that will be turned in, in 8 hours! We are stuck with this problem and thought of this place as a kind of last resort.
I'm trying to turn on/off a remotely controlled outlet with an Arduino.
We get inconsistencies when doing so. Sometimes when it goes into the if statement that ”should be ON” but instead turns the power off of the outlet and vice versa.
Code:
// on off remote control
int off = 12;
int on = 13;
void setup() {
pinMode(off, OUTPUT); // sets the digital pin as output
pinMode(on, OUTPUT);
}
void loop() {
// ..first we getting response from server if remote control should be on/off,
// working fine so not really relevant to problem.
// then we determine if outlet should be ON or OFF:
response.toCharArray(responseCharArray,100);
if(strstr( responseCharArray, "active") && strstr( responseCharArray, "1")) {
// This should turn ON the outlet.
digitalWrite(on, HIGH);
delay(250);
digitalWrite(on, LOW);
Serial.println("should be ON");
}
else if(strstr( responseCharArray, "active") && strstr( responseCharArray, "0")) {
// This should turn OFF the outlet.
digitalWrite(off, HIGH);
delay(250);
digitalWrite(off, LOW);
Serial.println("should be OFF");
}
}
Picture of the wiring:
Question:
What could be missing here? Since it randomly turns it on/off while entering same if statement.
There is too little information here.
Notice that your code will trigger (turn on) if responseCharArray contains something like e.g. "retroactively from 1941", or turn off for something like "active for 37 seconds".
In other words, that string-matching is not very precise, but it's hard to know what it should be since I don't know anything about the format of the response.
Perhaps it should at least be
if(strstr(responseCharArray, "active=1") != NULL)
or something, to at least lock the 1 to the active part.
In the logging that you do, print out the value of responseCharArray too. This will let you analyze whether or no the decision-making made sense.
Also, as always, triple-check your wiring and watch for e.g. back-feeding.

Touch sensor not working

I am attempting to create a very simple program in RobotC. In this program the robot will move forward until the touch sensor is hit.
#pragma config(Sensor, S2, touchSensor, sensorTouch)
void setMotors(int a, int b){
motor[motorA] = a;
motor[motorB] = b;
}
task main(){
wait1Msec(100);//Wait for sensor to init
setMotors(50, 50);
while(sensorValue(touchSensor) == 0){
//Do Nothing
}
setMotors(0, 0);
}
This code should make the robot move forward until the touch sensor is triggered.
Whenever I try and do anything with the touch sensor it does not work. When I output the value to the debug log it shows 180 when pressed and 1024 when released. I have verified that it is working normally by viewing the value on the brick itself.
Robot C Version: 4.0
Apparently, your touch sensor is stuck in SensorRaw mode. It is unclear - from the documentation I could find - how this could be fixed in code, but a work-around would be to explicitly put the sensor into raw mode (in case the situation changes in the future), and then compute the boolean value with a function like this:
bool sensorIsOn(short sensorRawValue)
{
bool isOn = false;
if(sensorRawValue > 512)
{
isOn = true;
}
return isOn;
}

Resources