Hi all!
First let me describe you my hardware. I have arduino, stepper motor driver and motor. With arduino I am controlling the speed of the motor through CLK on driver, the speed is selected with a rotary switch (works as voltage divider and I am reading value on analog pin), for start and stop I have two pushbuttons. Everything works fine to this point. But when I want to integrate another signal, this one is from pump through optocoupler, I don't receive just pulse but I have constant voltage for the time that pump runs. At this point stepper motor reaches max speed and starts accelarating again, and so on, same way as I would hold my start button. How would I be able to read only change of state and take that as a pulse?
int selectorPin;
int numOfSteps;
float divider;
int enable = 13;
int has1 =1;
int has2 =2;
int has3 =3;
int has4 =4;
int x;
int start = 8;
int start1;
int avtstop = 9;
int stop1;
int stopled = 6;
int startled = 7;
int memo = 0;
int opto = A3;
int sv;
void setup()
{
//pin za enable
pinMode(enable, OUTPUT);
pinMode(start, INPUT);
pinMode(avtstop, INPUT);
pinMode(stopled, OUTPUT);
pinMode(startled, OUTPUT);
pinMode(opto, INPUT);
digitalWrite(enable, LOW);
digitalWrite(stopled, HIGH);
//analogni pin za branje vrednosti stikalnega preklopnika
selectorPin = 0;
//stevilo preklopov stikalnega preklopnika
numOfSteps = 5;
//divider for decoding analog input
divider = 1023.0 / numOfSteps;
}
void loop()
{
//divide and round the input as float
float selectorValueFloat = round(analogRead(selectorPin) / divider);
//cast to integer for e.g. indexing arrays
sv = selectorValueFloat;
stop1 = digitalRead(avtstop);
start1= digitalRead(start);
if(sv ==0 || stop1 || memo !=sv)
{
noTone(12);
digitalWrite(enable, LOW);
digitalWrite(stopled, HIGH);
digitalWrite(startled, LOW);
has1 = 1;
memo = sv;
}
if(sv == 1){
memo=sv;
if(start1){
digitalWrite(enable, HIGH);
digitalWrite(startled, HIGH);
digitalWrite(stopled, LOW);
if(has1 == 1)
{
for(x = 200; x<=1850; x++)
{
tone(12,x);
delay(3);
has1 = 2;
has2 = 2;
}
}
else
{
tone(12, 1850);
has1 = 1;
}
}}
if(sv == 2){
memo=sv;
if(start1){
digitalWrite(enable, HIGH);
digitalWrite(startled, HIGH);
digitalWrite(stopled, LOW);
if(has2 == 2)
{
for(x = 200; x<=1100; x++)
{
tone(12,x);
delay(3);
has2 = 1;
has3 = 3;
has1 = 1;
}
}
else
{
tone(12, 1100);
has2 = 2;
}
}}
if(sv == 3){
memo=sv;
if(start1){
digitalWrite(enable, HIGH);
digitalWrite(startled, HIGH);
digitalWrite(stopled, LOW);
if(has3 == 3)
{
for(x = 100; x<=555; x++)
{
tone(12,x);
delay(3);
has2 = 2;
has3 = 2;
has4 = 4;
}
}
else
{
tone(12, 555);
has3 = 3;
}
}}
if(sv == 4){
memo=sv;
if(start1){
digitalWrite(enable, HIGH);
digitalWrite(startled, HIGH);
digitalWrite(stopled, LOW);
if(has4 == 4)
{
for(x = 200; x<=1850; x++)
{
tone(12,x);
delay(3);
has3 = 3;
has4 = 3;
}
}
else
{
tone(12, 1850);
has4 = 4;
}
}}
}
Thank you for your help and best regardd,
Alex