Help a noob in (probably) simple project

I´m trying to make a counter for how long anchor line I have out.
Get an impuls for every turn of anchor line
Managed to get it to work half way...and show on LCD: number of turns and translate to length but can not figure out how to make it count backwards when I receive the anchor..

I want to use another pin, (pin8) and when that is high the program should count backwards..

Code:


/*
 Räknare för impulser och längd på utmatad kätting ankarspel
Ankarspelet i detta exempel har NO magnetgivare med en impuls per varv, ett varv matar 24cm kätting.
räknaren börjar på minus 1,2 meter för att kompensera ankarets höjd över havsytan
impulsgivaren är ansluten till D6 via 22K resistor mot GND ,, D6 har pullup 680K mot Vcc
Resetknapp på D7 som har 22K resistor mot GND ,, D7  har pullup 680K mot Vcc

 2st - 22 kohm 
 2st - 680 kohm 
  - 10kohm potentiometer (kontrast Lcd)
  - 16x2 LCD screen
  -  switch (for reset)
  External magnetic switch

  created aug 2023
  by HL

*/
          
// include the library code:

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


int sigIn = 6; //pulled up signal with 680k resistor 
int resetBtn = 7; // pulled up to with 680k resistor 
int event;    
int eventLastState;
float langd;


// set up a constant for the impuls-switch pin
//h const int switchPin = 6;

// variable to hold the value of the switch pin
//h int switchState = 0;

// variable to hold previous value of the switch pin
//h int prevSwitchState = 0;


void setup() {
  // put your setup code here, to run once:
  pinMode(sigIn, INPUT); //event function Pulled up
  pinMode(resetBtn, INPUT); //resetting option 
  event = 0;
  eventLastState = 1;
  langd = 0;

  // set up the number of columns and rows on the LCD
  lcd.begin(16, 2);



  // Print a message to the LCD.
        lcd.setCursor(0, 0);

  lcd.print("      Redo      ");
        lcd.setCursor(0, 1);

  lcd.print(" Ankarlina  0m.");
 
}

void loop() {
  
  byte eventNewState = digitalRead(sigIn);
  delay(10); //test for debounce

  if (eventNewState == 0 && eventLastState == 1){
      eventLastState = eventNewState;
      event = event +1;
      langd = event *0.24-1.5;
      lcd.setCursor(0,0);
      lcd.print("Antal Pulser ");
      lcd.print(event);
      lcd.setCursor(0, 1);
      lcd.print("Antal meter ");
      lcd.print(langd);
      }
  
  else if (eventNewState == 1 && eventLastState == 0){
    eventLastState = 1;
    }
  
   byte ResetBtnState = digitalRead(resetBtn);
   if (ResetBtnState == LOW){
    setup();
    }
   
   }

Much to high, making it useless. The Arduino's internal pull-ups are around 50K, so no point adding external pull-ups that are higher than that. Consider 10K or 4K7.

1 Like

Welcome to the forum

Something along the lines of

if (digitalRead(8) == HIGH)
{
  event = event +1;
}
else
{
event = event -1;
}
2 Likes

You can simplify this to

  eventLastState = eventNewState;
1 Like

Waow This was quick response ! Thank you.

I will try to put that in the code (just gonna figure out where)
A new IF/ELSE ?

No, just put it where you currently have this line

event = event +1;

Thanks!

I will try that to, also change the pull-up to 10K TY!

You may not need the external pull-up at all if the wires are short and the environment is electrically quiet, if you use INPUT_PULLUP.

But maybe the wires are long and I guess there will be a powerful electric motor running nearby, so the external pull-ups are probably a good idea.

Here's a problem: I guess you will power the Arduino off the 12V power to the anchor winch? Then the Arduino will loose the value of event when you cut the power to the winch from the switches in the cabin. When you pull up the anchor, event will become negative.

Yes, probably best to use hardware pull-ups in this case, But very good to know for when Im sitting home and experiment, so I don't need all this resistors on breadboard.

Yes that is true, but I will take the 12V direct from battery so as long as I don't turn that switch of it will work, at the same time I want to draw as little as possible from the battery when anchoring so if I know I will be there for a while I will just have to take a note of how long line I have out

When Iv been experimenting I have only used the GND and 5V on the Arduino as inputs.

But can I have pin8 pulled down via say 22k.
and have it connected via resistor (10k) to the same 12V that powers the Arduino when I want it high or is that bad for the Arduino? (the 12V can go as high as 14,5 when I charge)
EDIT:
maby change so pull down with 10K and 22k to +12V should get about 4V on pin 8 when 12v activates..

You could save the data in Arduino's EEPROM memory.

The value of count could be saved to EEPROM every 30 seconds, but only if it has changed. This should ensure that the EEPROM lasts for many years (EEPROMcan only be updated about 100,000 times).

Why would you want that?

You are correct, that would be bad for the Arduino. You should connect the pull-up resistor to the 5V pin and the 12V to the Vin pin.

Care must be taken when powering Arduino with 12V because the Arduino's regulator can overheat and be damaged if too much current is drawn from its output. But with only a 16x2 LCD and a few buttons, that should be ok.

Correct in theory, but no need to take the risk. Connect any pull-up resistors to 5V and there is no risk.

Yes, today I have a small external voltage regulator 7808 so that the Arduino is powered with 8VDC on the VIN.
I have a 10K pulldown on pin8 to ground and want to use the signal from the 12V-motor when I retract the anchors, that's why Im thinking about connect a 22k resistor between the pin 8 and that 12V-terminal that goes high on motor when retract anchor.

Thanks a lot about info about EEPROM I will definitely read about and see if I get that to work.
...So I can write the info about event say like every 5-10 minutes if the info have changed to the EEPROM and receive/read that when I start the Arduino again.?

This is the pulse signal that indicates a rotation of the chain pulley?

It's it actually 12V? Have you measured it? Or are you assuming it's 12V because the motor is 12V?

It's quite likely that the output, which I assume is a pair of wires, connects inside the winch to either a mechanical microswitch or reed switch, or the Phototransistor of of an optical sensor, or to a magnetic hall effect sensor. In all those cases, the output may not have any voltage, it's just a pair of wires that are either connected or not connected. If I'm right, all you need is a pull-up pin (to 5V) on the Arduino pin, and the sensor wires connected between the Arduino pin and the Arduino's ground.

Can you find a link to the specs of the anchor winch which gives some detail about the pulse sensor?

Thanks a lot! Im impressed by the time people put in to help others.

That is correct, Its a magnetic sensor that just close for about 1 degree of 360 for each turn of the actual winch-wheel.
so that signal is not a problem, I have it connected to pin6 and it works fine
But to be able to count backwards as I got help with here
(to put an if 8==high inside the existing if) Got that to work...

It's a simple winch that only have 2 poles that get +/- or -/+. depending on lowering or receiving anchor.
So its either from that (+receive on motor) or maybe if I can find a + from the button that controls the relay.. either way Its a 12V (11-14,5)

I would suggest that you don't call setup from your program. There's stuff in there that you don't need to repeat - pinMode statements for example.

There is also a risk that there are things that you really don't want to do again e.g. lcd.begin. You will probably get away with it here, but some libraries allocate memory in their begin method. You may run out of RAM & crash.

I don't think it's likely here, but it's bad practice. Take the code in setup that you want to use for reset and put it in a different function. Call it from setup and your button push.

You could use a voltage divider (like 10K+10K+10K) to reduce the voltage from one of the motor connections down to 4~5V and connect that to Arduino pin 8. Might be a good idea to attach a 5.1V Zener diode from the Arduino pin to ground to protect it against any high voltage spikes in the 12V supply.

Alternatively, an opto-isolator (+series resistor like 2K2) between the two motor connections.

Ah! Yes of course, didn't think of that but a voltage divider is the simples way (TY for tips about zener.
Will do both of these to get it right.