First post = )
Hi,
This is my first attempt to use Arduino for something.
I use Arduino (UNO) one relay shield and one analog input, connected "all that" to my appartment interphone system.
The interphone system is two wire and not much of importance because the issue is not there I beleive but in the code.
Basically I have "attached" GND and A(0) to interphone speaker.
When interphone is IDLE voltage over speaker connection is ZERO. No activity intended in this state.
Second case (state) is when interphone is ringing the voltage on speaker connections is between 0.8 - 1.4 volts. At that moment arduino closes the relay and my building gate is opening for a delay(n).
Here is the code that is working as intended. I know that not all cases are covered but this works pretty fine for a few days now. I ring and then building door lock is open.
void setup()
{
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(4, LOW);
delay(1000);
}
void loop()
{
int voltage = analogRead(A1);
if (voltage > 60)
{
delay(3000);
digitalWrite(4, HIGH);
delay(6000);
digitalWrite(4, LOW);
}
}
I have a testing mechanism which is: When I lift a interphone "speaker and mic handle" the voltage is also present on the speaker. Then I close the handle and Arduino is opening the door again.
This way I can confirm that my code is working without going DOWN UP from 5th floor.
My issue is that when I upload this second version of code. It works only partially.
When I do the test with interphone handle everything is just fine.
But with the second version the actuall ringing from interphone button downstairs Arduino won't let me in. I am clueless how to debug this.
This is NOT working code. Please don't mind the Serial.println garbage.
void setup() {
Serial.begin(9600);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(4, LOW);
delay(1000);
}
unsigned long ringstart;
unsigned long ringidle;
unsigned long ringstop;
bool isringing() {
if (analogRead(A1) >40) {
return true;
} else {
return false;
}
}
void loop() {
Serial.println( ringstop - ringstart );
Serial.println(analogRead(A1));
ringidle = millis();
while (isringing()) {
ringstart = ringidle;
ringstop = millis();
}
Serial.println(ringstart);
Serial.println(ringstop);
Serial.println(ringstop - ringstart);
if ( ringstop - ringstart >= 50 ) { \\Don't sense shorter button press less than 50ms
while (ringstart == ringidle) {
while (ringstop + (ringstop - ringidle) > millis()) { \\Keep relay opening the dor for the duration of ring button pressed
digitalWrite(4, HIGH);
Serial.println(ringstart);
Serial.println(ringstop);
}
ringstop = -1;
ringstart = -1;
}
}
else {
digitalWrite(4, LOW);
ringstart = -1;
ringstop = -1;
}
}