I succeeded in detecting a change in state by obtaining a 24 VAC signal by referring to the forum above.
Note:
DIGITALWRITE(PIN13) = internal LED UNO
DIGITALWRITE (PIN12) = External LED on Breadboard
I want to do this.:
Internal LED ON (PIN13) when 24 VAC is supplied to the first optical coupler.
External LED ON (PIN12) when 24 VAC is supplied to the second optical coupler.
Built-in LED off if no current flows through the second optical coupler (PIN13)
Built-in LED off (PIN13) if no current flows through the first optical coupler
The problem is as follows:
One more optical coupler. Therefore, two source codes were duplicated.
The first optical coupler is the same as my expectations.
The second optical coupler does not detect any state changes.
THANKS!
const int sensorPin = 10;
const int sensorPin1 = 11;
const int ledPin = 13;
const int ledPin1 = 12;
int lastSensorState;
int currentSensorState;
int stateChangeCnt = 0;
int lastSensorState1;
int currentSensorState1;
int stateChangeCnt1 = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(sensorPin, INPUT);
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, LOW);
pinMode(sensorPin1, INPUT);
Serial.begin(9600);
}
void loop()
/////////// 1st optocoupler
{
currentSensorState = digitalRead(sensorPin);
if (currentSensorState != lastSensorState)
{
Serial.print(stateChangeCnt);
Serial.print(" : ");
Serial.print(currentSensorState);
Serial.print(" : ");
Serial.print(lastSensorState);
Serial.print("\r\n");
if (currentSensorState == HIGH )
digitalWrite(ledPin, LOW);
else if
(currentSensorState != HIGH )
digitalWrite(ledPin, HIGH );
lastSensorState = currentSensorState;
stateChangeCnt++;
/////////// 2nd optocoupler
currentSensorState1 = digitalRead(sensorPin1);
if (currentSensorState1 != lastSensorState1)
{
Serial.print(stateChangeCnt1);
Serial.print(" : ");
Serial.print(currentSensorState1);
Serial.print(" : ");
Serial.print(lastSensorState1);
Serial.print("\r\n");
if (currentSensorState1 == HIGH)
digitalWrite(ledPin1, LOW);
else if
(currentSensorState1 != HIGH )
digitalWrite(ledPin1, HIGH);
lastSensorState1 = currentSensorState1;
stateChangeCnt1++;
}
}
}
First of all, I'm not clear about whether you say now it's working? If not... Okay I need to back up. Have you tried simple sketches that prove that both sensors are working? You said the second one did not, so that is a good place to start. Eliminate the code that processes the sensor data, just echo it somehow, like serial. It sounds like you already tested both LEDs?
I don't know what a footnote line is. Did you try auto-formatting your code to highlight the code blocks? In the lines you've highlighted with a '**', there is an extra '{'. Maybe that has something to do with it.
It's instructive, as you say, when I took a step back and looked at the code again, I found a couple of screws missing.
The screws were "{" and "}" in the middle of the code.
const int sensorPin = 10;
const int sensorPin1 = 11;
const int ledPin = 13;
const int ledPin1 = 12;
int lastSensorState;
int currentSensorState;
int stateChangeCnt = 0;
int lastSensorState1;
int currentSensorState1;
int stateChangeCnt1 = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(sensorPin, INPUT);
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, LOW);
pinMode(sensorPin1, INPUT);
Serial.begin(9600);
}
void loop()
/////////// 1st optocoupler
{
currentSensorState = digitalRead(sensorPin);
if (currentSensorState != lastSensorState)
{
Serial.print(stateChangeCnt);
Serial.print(" : ");
Serial.print(currentSensorState);
Serial.print(" : ");
Serial.print(lastSensorState);
Serial.print("\r\n");
if (currentSensorState == HIGH )
digitalWrite(ledPin, LOW);
else if
(currentSensorState == LOW )
digitalWrite(ledPin, HIGH );
lastSensorState = currentSensorState;
stateChangeCnt++;
}
{
/////////// 2nd optocoupler
currentSensorState1 = digitalRead(sensorPin1);
if (currentSensorState1 != lastSensorState1)
{
Serial.print(stateChangeCnt1);
Serial.print(" : ");
Serial.print(currentSensorState1);
Serial.print(" : ");
Serial.print(lastSensorState1);
Serial.print("\r\n");
if (currentSensorState1 == HIGH)
digitalWrite(ledPin1, LOW);
else if
(currentSensorState1 == LOW )
digitalWrite(ledPin1, HIGH);
lastSensorState1 = currentSensorState1;
stateChangeCnt1++;
}
}
}