[SOLVED} Geartooth sensor for a reloading press coding

Hey everyone!

I’m working on a geartooth sensor for a reloading press and having some coding issues. I’m sure it’s a simple fix but I’m new at this…
My sensor connected to a Uno on a digital pin works fine and gives a high/low value depending on the sprocket teeth position. If the value of the sensor doesn’t change for > 1sec… then the gear is not turning (a Jam occured) and the Uno will trigger a relay and cut power to the motor…

Here’s the code…It’s my first project. If someone could point out the obvious to me I’m a bit stuck and the more I tinker with the code the deeper I get…

Thanks in advance!!

Eric

 int sensePin =2;
int relayPin =8;


void setup(){
  pinMode(sensePin, INPUT);
  
  Serial.begin(9600);
}

void loop() {
  Serial.println(digitalRead(sensePin));
  delay(1000);

currentValue = sensPin();

unsigned long timer = millis();
  if(currentValue != oldValue && millis() - timer < 1000) // 1000 = 1 second
  {
    oldValue = currentValue; // they were different and values changed within 1 second, so update old value for next reading
    Serial.println("Oscillating!  "); // show a message that it was oscillating
    timer = millis();                        // Reset timer
  
if(millis() - timer >= 1000); //trigger relay if sensor value is constant for greater than 1 sec
{
  digitalWrite(relayPin, HIGH); //turn on relay
}
  // set an output pin high
}

Here's the code

The code doesn't even compile.

sketch_oct25a.ino: In function 'void loop()':
sketch_oct25a:15: error: 'currentValue' was not declared in this scope
sketch_oct25a:15: error: 'sensPin' was not declared in this scope
sketch_oct25a:18: error: 'oldValue' was not declared in this scope
sketch_oct25a:29: error: expected `}' at end of input

Is that the issue that you are having? If not, where is the code that compiles, and what is the issue? What you've posted so far is like saying "my car won't start. It's red".

currentValue = sensPin();

Let's start with this. Where have you declared that currentValue is a variable?

Where have you defined the sensPin() function you are trying to call?

Thanks for responding...I found a video and I'll clean up a few things and then come back....I see what you're referring to....I'll be back in a few

Thanks Eric

 int sensePin =2; //gear sensor 5v
int relayPin =8;


void setup(){
  pinMode(sensePin, INPUT);
  
  Serial.begin(9600);
}

void loop() {
  Serial.println(digitalRead(sensePin));
  delay(1000);

int currentValue = (digitalRead(sensePin));  //should read the High/Low from sensor

unsigned long timer = millis();
  if(currentValue != oldValue && millis() - timer < 1000) // 1000 = 1 second
  {
    oldValue = currentValue; // they were different and values changed within 1 second, so update old value for next reading
    Serial.println("Oscillating!  "); // show a message that it was oscillating
    timer = millis();                        // Reset timer
  
if(millis() - timer >= 1000); //trigger relay if sensor value is constant for greater than 1 sec
{
  digitalWrite(relayPin, HIGH); //turn on relay
}
  // set an output pin high
}

I fixed currentvalue but I’m not sure how to handle old value…I’m trying to compare states of sensor and take action if state does not change for 1 sec…

Eric

I fixed currentvalue but I'm not sure how to handle old value

The easiest way is to define a global variable, called oldValue. At the end of loop(), assign oldValue to value that is in currentValue.

Get rid of the delay() call at the top of loop.

timer is a lousy name for an interval. It needs to be a global variable, too, of type unsigned lomg.

Use Tools + Auto Format, and make more of an effort to keep the code properly indented.

Thanks for spoon feeding me Paul....I'll make those corrections and return!

Thanks again

E

@spitboy, I would like to know what gear tooth sensor you are using?

...R

http://www.cherrycorp.com/english/sensors/pdf/GS1005-GS1007_Series.pdf

I got the GS1007 sensor 5V Hall Sensor 3 wire sinking

Eric

PaulS:

I fixed currentvalue but I’m not sure how to handle old value

The easiest way is to define a global variable, called oldValue. At the end of loop(), assign oldValue to value that is in currentValue.

Get rid of the delay() call at the top of loop.

timer is a lousy name for an interval. It needs to be a global variable, too, of type unsigned lomg.

Use Tools + Auto Format, and make more of an effort to keep the code properly indented.

I changed a few things and got the sketch to compile…I’m wanting to compare the oldvalue to the currentvalue of the sensor and take action if the state is not changing after 1 sec…

Can anyone see if my code will do this?

Thanks

Eric

int sensePin =2; //gear sensor 5v digital
int relayPin =8; //coil of micro relay to cut power


void setup(){
  pinMode(sensePin, INPUT);

  Serial.begin(9600);


}

void loop() 
{
  Serial.println(digitalRead(sensePin));


  int currentValue = (digitalRead(sensePin));  //should read the High/Low from sensor

  unsigned long timer = millis();
  
  int oldValue =(digitalRead(sensePin));      // initial reading from sensor to begin a comparison 
  
  if(currentValue != oldValue && millis() - timer < 1000) // 1000 = 1 second
  {
    oldValue = currentValue; // they were different and values changed within 1 second, so update old value for next reading
    Serial.println("Oscillating!  "); // show a message that it was oscillating
    timer = millis();                        // Reset timer

    if(millis() - timer >= 1000) //trigger relay if sensor value is constant for greater than 1 sec

      digitalWrite(relayPin, HIGH); //turn on relay


  }

}
  int currentValue = (digitalRead(sensePin));  //should read the High/Low from sensor

The paren in front of digitalRead(), and the one before the ; are not needed.

  int oldValue =(digitalRead(sensePin));      // initial reading from sensor to begin a comparison

No. The two digitalRead()s will happen very close together. They are unlikely to ever be different.

Look at the state change detection example. It shows, properly, how to detect a state change.

Once the state change has been detected, not before then, record when that happened.

Periodically (on every pass through loop()), see if it has been too long since a state change happened.

PaulS:

  int currentValue = (digitalRead(sensePin));  //should read the High/Low from sensor

The paren in front of digitalRead(), and the one before the ; are not needed.

  int oldValue =(digitalRead(sensePin));      // initial reading from sensor to begin a comparison

No. The two digitalRead()s will happen very close together. They are unlikely to ever be different.

Look at the state change detection example. It shows, properly, how to detect a state change.

Once the state change has been detected, not before then, record when that happened.

Periodically (on every pass through loop()), see if it has been too long since a state change happened.

I found the state change detection example and copycatted it a bit…I still struggle at combining a timer with the state change…On the serial monitor I can keep a count as I trigger the sensor but wanting to trigger a relay if the state does not change after 1 sec…

here’s where I’m at - Thanks Eric

 int ledPin = 13;       // the pin that the LED is attached 

int sensePin =2; //gear sensor 5v digital
int relayPin =8; //coil of micro relay to cut power

int sensorcounter = 0;   // counter for the sensor
int currentstate = 0;         // current state of the sensor
int laststate = 0;     // previous state of the sensor

void setup() {
  // initialize the sensor  as a input:
  pinMode(sensePin, INPUT);
  
  pinMode(relayPin, OUTPUT); //relay output
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the initial state of sensor
  currentstate = digitalRead(sensePin);

  unsigned long timer = millis();

  // compare the currentstate to its previous state
  if (currentstate != laststate) {
    // if the state has changed, increment the counter
    if (currentstate == HIGH) {
      // if the current state is HIGH then the sensor
      // went from low to high:
      sensorcounter++; 
      
      Serial.print("number of button pushes:  ");
      Serial.println(sensorcounter);
    } 

    if(currentstate != laststate && millis() - timer < 1000) // 1000 = 1 second
  
      digitalWrite(ledPin, HIGH);
    
      laststate = currentstate;




  }
}

Thanks for the link to the sensor you are using.

I think the logic of your program needs to be like this (pseudo code)

setup  {
  initialize stuff, including setting oldvalue to 0
}

loop {
   newvalue =  read sensor
   if newvalue != oldvalue {
       oldvalue = newvalue
       valueChangeTime = millis()
   }
   else { // if newvalue == oldvalue
       if (millis() - valueChangedTime > than allowedTime {
           switch on/off the relay
       }
   }
}
  unsigned long timer = millis();

Unconditionally setting the start time is wrong. Set this (and it needs to be a global variable) only when the transition to pressed occurs. And, timer is a lousy name. startTime would be better.

lastState needs to be set to currentState on EVERY pass through loop().

PaulS:

  unsigned long timer = millis();

Unconditionally setting the start time is wrong. Set this (and it needs to be a global variable) only when the transition to pressed occurs. And, timer is a lousy name. startTime would be better.

lastState needs to be set to currentState on EVERY pass through loop().

Thanks for responding Paul...If it's not too much trouble could you help me with the coding...I really am stuck...I'll change the name of the timer too!!

Thanks
Eric

This code should turn the LED on when the switch becomes pressed, and turn it off one second later, regardless of how long you hold the switch.

 int ledPin = 13;       // the pin that the LED is attached 

int sensePin =2; //gear sensor 5v digital
int relayPin =8; //coil of micro relay to cut power

int currentstate = 0;         // current state of the sensor
int laststate = 0;     // previous state of the sensor

unsigned long startTime = 0;

void setup() 
{
  // initialize the sensor  as a input:
  pinMode(sensePin, INPUT);
  
  pinMode(relayPin, OUTPUT); //relay output
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
 {
  // read the initial state of sensor
  currentstate = digitalRead(sensePin);

  // compare the currentstate to its previous state
  if (currentstate != laststate) 
  {
    // if the state has changed, increment the counter
    if (currentstate == HIGH) 
    {
      // if the current state is HIGH then the sensor
      // went from low to high:

      digitalWrite(ledPin, HIGH);
      startTime = millis();
   } 

   if(startTime > 0 && millis() - startTime < 1000)
   {
      digitalWrite(ledPin, LOW);
      startTime = 0;
   }

   laststate = currentstate;
}

BIG THANKS TO “PAULS” and “ROBIN2”

Got the code working with my geartooth sensor…after 1 second of the sensor not changing state a LED (soon to be relay) is triggered…

Thanks for your help on my first project on an Arduino

Thanks Again! I learned a lot!!!

Eric
Here’s the sketch that I settled on:

//sorry if my format isn't perfect...My first project

int sensePin =2; //gear sensor 5v digital
int relayPin =13; //coil of micro relay to cut power
int oldvalue = 0; //previous sensor state
int newvalue = 0; //current state of sensor
unsigned long valuechangetime; //timer 

void setup(){
  oldvalue = 0; //previous sensor state
  pinMode(relayPin, OUTPUT);  
  pinMode(sensePin, INPUT);
  Serial.begin(9600);
}

void loop() 
{
  newvalue = digitalRead(sensePin); //read current state of sensor

  if (newvalue != oldvalue)
  {
     valuechangetime=millis();
     
     oldvalue=newvalue;
      
  }   
   else if  (millis() - valuechangetime > 1000)
   {
     digitalWrite(relayPin, HIGH);
     
   }
    
}