Make two statuses on the same analog reading

Hello there,

I have a machine which i want to read the analog reading (voltage) that will triggered only 2 (two) times each machine’s cycle. The first trigger is when machine start, it will make relay ON around 500ms and then back to Off. There will be voltage spike from 0V to 12 V DC. The second trigger will occur when the machine’s cycle goes Stop. It will be also voltage spike from 0V to 12V DC.
I am using this sensor to read voltage of the relay’s spike. The controller i used is NodeMCU.
My code for voltage function is look like this :

void voltage() {
// read the value at analog input
value = analogRead(voltagePin);
vout = (value * 5.0) / 1024.0;
vin = vout / (R2/(R1+R2));
Serial.print("INPUT V= ");
Serial.println(vin,2);
if(vin>=2) { //Start to read K11’s relay energize on inverter
machineText = inUseText;
}
delay(100);
if(vin<2) {
machineText = readyText;
}
}

That code only works when the relay is in constant On and constant Off.
How can i read the machine status (Ready & in use), when the relay have such kind of behaviour?

Any suggestions will be appreciated.

Hi @astonix, not sure I understand your question.

Do you want to show a instant status of your machine? i.e., in a dashboard? If this is the case, you can use the concept of “streaming resources”. Just define a resource with your machine text, and send this to the platform when you detect the change (or you activate the relay). Take a look to the documentation:

http://docs.thinger.io/arduino/#coding-streaming-resources

Okay, actually this is door lock method for the machine which use relay, PTC and solenoid to lock/unlock the door. If only to shows machine status using resources, i knew it. But, what i want to ask is how can i read machine status with behaviour like this?
The machine’s workflow :

  1. When we push start button, it will energize the relay and will trigger voltage spike 12V DC around 500ms, then the relay is back to Off (0V DC). The door is locked. On the thinger dashboard it should show “machine is in use” text.
  2. The machine will do the process cycle around 60 minutes.
  3. After the process is finished, it will also energize the relay and will trigger voltage spike 12V DC around 500ms, then the relay is back to Off (0V DC). The door is now unlocked. On the thinger dashboard it should show “machine is ready” text.

Regards,
Aston

As I mentioned, this can be done with streaming resources, to see immediate changes of the working condition on the dashboard.

Assuming your voltage code works, this is the general idea (not tested):

bool inUse = false;

void setup() {
  thing["state"] >> [](pson& out){
    out = inUse ? "Machine Working" : "Machine Ready";
  };
}

void loop() {
  thing.handle();
  voltage();
}

void voltage() {
  value = analogRead(voltagePin);
  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2/(R1+R2));
  if(vin>=2 && !inUse){
      inUse = true;
      thing.stream("state");
  }else if(vin<2 && inUse){
      inUse = false;
      thing.stream("state");
  }
}

Hi,

Have tried that code, and the result is : When machine start, it detect the voltage spike and shows “machine working” but just for a while (around 500ms) then the text is back to “machine ready”.
I want to make “machine working” text message appear during machine’s process cycle, and disappear when it finished.

You should implement some kind of state control:


int state = 0;

void setup() {
  thing["state"] >> [](pson& out){
    out = sate==0 ? "Machine Ready" : "Machine Working";
  };
}

void loop() {
  thing.handle();
  voltage();
}

void voltage() {
  value = analogRead(voltagePin);
  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2/(R1+R2));

  // voltage detected
  if(vin >= 2){
    if(sate==0){
      state = 1;
    }else if(state==2){
      state = 3;
    }
  // no voltage
  }else{
    if(sate==1){
      state=2;
      thing.stream("state");
    }else if(state==3){
      state=0;
      thing.stream("state");
    }
  }
}

Hi @alvarolb

I’ve tried your code, make a simulation using 12V DC power supply, and it works. But, i forgot that when the machine’s relay is Off, the voltage is not realy 0, it is around 600mV - 1,4V DC.
Thus, when i tried on real machine, the result is :
When the machine start, relay is On, and voltage spike 12V DC, it shows “Machine working”. But some times the text changes to “Machine Ready”. It is because of the voltage on the relay is floating around 600mV - 1,4V DC.

How to catch the machine status when the machine’s behaviour is like that?

Hi,

It should work with that code, because the condition is not 0V, is every voltage under 2V, so if there is a floating voltage under 2V it shouldn’t trigger the state machine.

You can try raising that 2V to 10V (for example) if you can warrantee that the condition peak is 12V

If you keep receiving a bad behavior, maybe another phenomenon is afecting the state machine, when a mechanical switch is triggered, imagine the switch in slow motion and just before the physical contact success, a very little spark jumps between the contacts surfaces, the electronic component detects it, sounds crazy but this phenomenom can trigger logical gates and, of course, the microcontroler inputs.

How to sort it?
I can
In physical circuit: Installing a capacitor between the two contacts of the switch (or relay, in this particular case), the value should be calculated with the equivalent circuit, to avoid another unexpected behavior of the circuit with this component added.

In code: The condition will be valid only when the time is> 50ms (for example), so if there is a pulse of 1uS (caused by a spark), it can be detected, but it won’t be considered as the condition and should not trigger the status machine.

Any of both should disipate the effect caused by this phenomenon, I recommend the code solution.

Hope this helps

I want to send the data of vin ,after the calculation. So suggest me the code.
Api---->> vin
Need the calculated
value = analogRead(voltagePin);
vout = (value * 5.0) / 1024.0;
vin = vout / (R2/(R1+R2));
Need to send the data of vin to api