Hi!!!
I'll cut the chase and explain my problem.
This is my goal.
Audio signal from headphone jack will trigger the relay for about 30seconds.
When i call the phone, the audio signal will trigger the relay. Laptop cooler fan will be triggered by relay.
i wanted that even if i cancel the call the relay will stay on for 30seconds, then shuts down.
I have limited resources, i only have arduino, relay shield, audio jack and a phone.
What i accomplish:
Connect audio jack to phone.
Then i connect the wire going to gnd and analog input pin 0.
AnalogRead shows 0 when there is no music played then it shows 0-58 range when a song is played.
For the mean time, i use led pin13 on the board. It works, it only lights up if the audiosignal is > 0. So No light when no song is played, then it blinks when i played a song.
Main issue, led doesnt stay on for 5seconds, when i add delay, what changes is the frequency of time it reads, not the time led stays on.
If i use this configuration, the relay will go crazy turning on and off.
I wanted it to activate my laptop fan cooler for 30 seconds when i call.
I hope you could help me.
Thanks!!!!
pseudo 'ish code:
Read analogue signal
if (analogue signal peaks above 40)
{
set timer to 30 secs
}
if( timer is more than 30 sec )
{
maintain HIGH on relay pin
}
else
{
set to low
}
timer1 -= millis();
Tammy tam!!! I'll post the code i'm using, can you help edit it for me? Like how to add your code there? Pin configuration confuses me.
Why does pin configuration confuse you?
Pin configuration is one of the first things you learn on this platform. You said you've managed to wire up and setup an analog input pin, but still I suggest you have a look at some examples to get a feel for setting up pins in different modes and then reading from them or writing to them.
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
int ledPin = 13;
// print out the value you read:
if(sensorValue > 0) digitalWrite(ledPin,HIGH);
else digitalWrite(ledPin, LOW);
Serial.println(sensorValue);
delay(500); // delay in between reads for stability
}
that is my setup so far...
When you post code, post it between `` tags, you can also use the button in the editor '</>'
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
int ledPin = 13;
// print out the value you read:
if(sensorValue > 0) digitalWrite(ledPin,HIGH);
else digitalWrite(ledPin, LOW);
Serial.println(sensorValue);
delay(500); // delay in between reads for stability
}
my bad, sorry...
Not tested, but should keep the led on for 30 seconds after song is played.
float g_timer = 0.0f;
void loop()
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
int ledPin = 13;
if(sensorValue > 40) // 40 might need tuning depending on your song/ringtone or whatever.
{
g_timer = 30000; // 30 seconds ...
}
g_timer -= millis();
if( g_timer > 0.0f )
{
digitalWrite(ledPin,HIGH);
}
else
{
g_timer = 0.0f;
digitalWrite(ledPin,LOW);
}
}
this is what it shows when i compile the code you sent me...
core.a(main.cpp.o): In function main': C:\Users\Gaby\Desktop\dementia\gmduino\arduino-1.0.4\hardware\gizduino-\cores\gizduino/main.cpp:11: undefined reference to
setup'
Yes, use my loop() with your setup ...
What is this project for? Is it for an assignment?
sorry i don't understand what you mean by use your loop() with my setup...
there is an error in compiling...
yes, we propose a system that will showcase what computers can do...
tammytam:
Not tested, but should keep the led on for 30 seconds after song is played.
float g_timer = 0.0f;
void loop()
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
int ledPin = 13;
if(sensorValue > 40) // 40 might need tuning depending on your song/ringtone or whatever.
{
g_timer = 30000; // 30 seconds ...
}
g_timer -= millis();
if( g_timer > 0.0f )
{
digitalWrite(ledPin,HIGH);
}
else
{
g_timer = 0.0f;
digitalWrite(ledPin,LOW);
}
}
That won't even compile, much less do what it's supposed to do....
Regards,
Ray L.
The setup() you posted earlier, with the loop() I just posted now! Like this!
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
float g_timer = 0.0f;
void loop()
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
int ledPin = 13;
if(sensorValue > 40) // 40 might need tuning depending on your song/ringtone or whatever.
{
g_timer = 30000; // 30 seconds ...
}
g_timer -= millis();
if( g_timer > 0.0f )
{
digitalWrite(ledPin,HIGH);
}
else
{
g_timer = 0.0f;
digitalWrite(ledPin,LOW);
}
}
yes, we propose a system that will showcase what computers can do...
For university / college?
it compiled! i uploaded it already!
what happens is it stays on,,, and doesn't blink. but when i ended the call, it turned the led off as well...
tammytam:
Enlighten me.
Well, let's see....
float g_timer = 0.0f;
Why on earth use a float to measure time in milliseconds? This drags in a lot of completely unnecessary code, wastes a lot of CPU cycles, and loses resolution, all for no benefit. millis() returns an unsigned long, not a float.
g_timer -= millis();
Think about what happens if the program starts, nothing happens for, say 31 seconds, THEN the tone is heard. How long will the LED be lit?
Regards,
Ray L.
RayLivingston:
float g_timer = 0.0f;
Why on earth use a float to measure time in milliseconds? This drags in a lot of completely unnecessary code, wastes a lot of CPU cycles, and loses resolution, all for no benefit. millis() returns an unsigned long, not a float.
Point taken, joys of working on a completely different platform where I measure fractions of a second as a float. Does however compile, and does function if not optimally.
g_timer -= millis();
Think about what happens if the program starts, nothing happens for, say 31 seconds, THEN the tone is heard. How long will the LED be lit?
Regards,
Ray L.
You're going to have to give it to me straight, I'm not seeing this.
g_timer -= millis();
this code makes led turn on when i make the call, but turn it off when i end the call.
g_timer == millis();
then i made it to this,
i turn the led on, but didnt turn off after 30seconds.
iceratio:
g_timer -= millis();
this code makes led turn on when i make the call, but turn it off when i end the call.
g_timer == millis();
then i made it to this,
i turn the led on, but didnt turn off after 30seconds.
No, that decrements the timer. You don't want to change it to g_timer == millis(), thats an equality operator. Try this:
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
unsigned long g_timer = 0;
bool trigger = false;
void loop()
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
int ledPin = 13;
if(sensorValue > 40) // 40 might need tuning depending on your song/ringtone or whatever.
{
g_timer = millis();
trigger = true;
}
if( trigger )
{
if( millis() - g_timer > 30000 )
{
digitalWrite(ledPin,LOW);
trigger = false;
}
else
{
digitalWrite(ledPin,HIGH);
}
}
}
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
unsigned long g_timer = 0;
bool trigger = false;
void loop()
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
int ledPin = 13;
if(sensorValue > 0) // 40 might need tuning depending on your song/ringtone or whatever.
{
g_timer = millis();
trigger = true;
}
if( trigger )
{
if( millis() - g_timer > 5000 )
{
digitalWrite(ledPin,LOW);
trigger = false;
}
else
{
digitalWrite(ledPin,HIGH);
}
}
}
IT WORKS!!!!!! the good thing is, i can extend the time on how long it turns on. this is what happens.
i make the call then the led,high. then after i cancel the call it counts 5sec then it turns off.
the good thing is i can make it stay on as long as i am not ending the call.
thankyou so much!!!! i just ordered the relay shield so i could try it on friday, thank you tammytam!!!