Could someone check this code?
this first attempt @ project (outside of messing examples). not sure if code/logic out lunch or if hardwired audio detection circuit. any , comments appreciated. i appologize messy code, first draft, since not work... lol... might last draft.
please see thread details on project
edit: updated code (last few lines not fit on post)
please see thread details on project
edit: updated code (last few lines not fit on post)
quote
/***************************************************************************************************
ipod-radio switcher
date:
august 7, 2010
hardware setup:
audio signal detector attached to analog pin 2
pushtotalk attached to digital pin 1
headphones relay attached to digital pin 7
mic relay attached to digital pin 8
rgb led connectied to pins 9, 10, 11
r = 9, g = 10, b = 11
comments:
audio detection works great!
to do list:
1. balance pnp (no idea how to do that one yet)
2. auto calibrate 'threshold'
-this should solve the problem of changing volume settings
-i think i could seperate high and low readings and find the range of each group then auto pick a spot between them both
3. build prototype
4. print pcb board
***************************************************************************************************/
/***************************************************************************************************
user defined constants
set these values below
\ / \ / \ / \ / \ / \ /
v v v v v v
***************************************************************************************************/
const int timer = 10000; //wait 10 seconds after signal detected before going ipod
const int threshold = 10; //an arbitrary threshold level that's in range of analog input
const boolean debugon = true; //set true debuging data sent on serial connection
/***************************************************************************************************
^ ^ ^ ^ ^ ^ ^ ^
/ \ / \ / \ / \ / \ / \ / \ / \
do not mess with anything below this line
***************************************************************************************************/
const int headphonepin = 2; //analog pin 2 listens signal txrx
const int micpin = 1; //digital pin 1 listens mic pressed
const int relayheadphones = 7;
const int relaymic = 8;
//*** declare varables ***
boolean ipod = true; //set ipod true when want ipod work
boolean txrx = false; //set txrx true when want switch txrx
unsigned long detecteduntil; //time in mills can turn ipod on
int val = 0; //used store temp integers.
/****************************************
setup runs once when first started
initialize pins and variables
****************************************/
void setup(){
// initialize pins
pinmode(relaymic, input);
pinmode(13, output);
// initialize serial communications:
if (debugon) {
serial.begin(9600);
} //end: if (debugon)
// initialize variables
detecteduntil = millis();
ipod = true;
txrx = false;
setrelays(); //initialize relays correct state
} //end: setup()
/****************************************
this is the main loop that will
keep repeating
****************************************/
void loop(){
if (signalfrommic() || signalfromtxrx()) {
signaldetected();
} else {
if (millis() > detecteduntil) {
if (!ipod) {
ipod = true;
txrx = false;
setrelays();
}
}
}
if (debugon) {
debugmsg();
}
} //end: loop()
/****************************************
signalfromtxrx()
checks if signal detected from txrx
uses threshold to determin result
****************************************/
boolean signalfromtxrx(){
val = analogread(headphonepin);
if (val > threshold) {
return true;
} else {
return false;
}
} //end: signalfromtxrx()
/****************************************
signalfrommic()
checks if mic button is pressed (from txrx)
returns true if pin is low (grounded out)
****************************************/
boolean signalfrommic(){
if (digitalread(micpin) == low) {
return true;
} else {
return false;
}
} //end: signalfrommic()
/****************************************
signaldetected()
handles event when signal is detected
****************************************/
void signaldetected(){
detecteduntil = millis() + timer;
if (ipod == true) {
ipod = false;
txrx = true;
setrelays();
}
} //end: signaldetected()
/****************************************
turn relays on or off (using pnp)
high = relay off
low = relay on
****************************************/
void setrelays(){
if (ipod) {
digitalwrite(relayheadphones,high);
digitalwrite(relaymic,high);
digitalwrite(13,low);
} else {
digitalwrite(relayheadphones,low);
digitalwrite(relaymic,low);
digitalwrite(13,high);
}
} //end: setrelays()
/****************************************
debugmsg()
this sends debuging info over the
serial port
****************************************/
//*** send debuging data to the serial port
void debugmsg(){
// serial.print("time: ");
code: [select]
void loop(){
if (signalfrommic) {
signaldetected();
} else {
if (signalfromtxrx) {
signaldetected();
} else {
if (millis() > detecteduntil) {
if (!ipod) {
ipod = true;
txrx = false;
setrelays();
}
}
}
}
if (debugon) {
debugmsg();
}
} //end loop()signalfrommic name of function. acts, way using pointer function. so, of course pointer has value, if statement true.
you not, however, calling function.
the same holds true signalfromtxrx.
as result, else clause skipped, , signaldetected function ever called.
adding () after signalfrommic , signalfromtxrx might change behavior of program.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Could someone check this code?
arduino
Comments
Post a Comment