Coding help please.

Hello,

Very very new to any of this, this is not my wheel house what so ever, but I hope I'm not overstepping.

I am having a very hard time even trying to learn any coding, and looking for some help.

I am currently trying to build a trolling motor throttle control setup. I have simulated an operating board, potentiometer and a servo motor. It is wired pot to Analog Pin 1, servo wired to PWM Output 9.

Current coding is the Arduino prebuilt example, "Servo knob".

Currently working as I would expect. However, my end goal is to add a few functions, in which i have no idea where to start. Hopefully this can be explained easy enough, so here goes.

Idle/Resume function.

--Servo is controlled by potentiometer to desired position. (Trolling engine set to demanded rpm)
--With use of push button, servo returns to idle position overriding pot position. (trolling engine idles)
---->Push of same button (if momentary button) or unlatch button, servo returns to
setting defined by pot. (Trolling engine resumes speed.)

Hunting function

--Again, potentiometer is defining servo position (engine speed).
--Pushing of momentary or latching push button (switch), servo maintains potentiometer for defined time (IE 50 seconds) then increases by a defined amount (IE 20 percent) for a defined amount of time ( IE 10 seconds) and then return to potentiometer defined setting, and the cycle repeat.

Also, I want to code a second switch/button to do the exact same, but only decrease by the defined amount. The same time frames would be used.

I hope this makes sense, and again hope I'm not overstepping. I have a decent understanding of electronics and theory, as a career mechanic. However, creating logic/code is beyond my understanding.

Thanks a pile!

Bob

I always suggest a person starts with a basic tutorial and learn how to do a few simple things, that shpuld help alot

Then see how to use this Forum at the top and the part about how to post your code using code tags

Also post a wiring diagram - a hand sketch is fine

This is a good place to get help as you go along - but don't expect someone to design and code your project for you unless you are willing to pay for their time

Also try the search function for the Forum - someone else has probably tried something similar

I will try and learn as I can, however I am certainly not against paying for a decent product.

bobhodder:
however I am certainly not against paying for a decent product.

If you just want someone to write a program for you for pay then please ask in the Gigs and Collaborations section of the Forum

The extra capabilities that you want to add should not be too difficult.

You need to have a variable that holds the speed value. It may get that from the potentiometer or it may be either of two preset values selected by pressing the buttons. It will be easier if you have latching switches or toggle switches. Something like this pseudo ccode

potentiometerValue = analogRead(potPin);
idleButtonValue = digitalRead(idleButtonPin);
huntButtonValue = digitalRead(huntButtonPin);

speedvalue = potentiometerValue;

if (idleButtonValue == LOW) { // assumes LOW when pressed
   speedValue = idleValue;
}

if (huntButtonValue == LOW) {
   // code for calculating hunt value
   speedValue = huntValue;
}

// code to make motor operate at speedValue

...R

Awesome! Thanks

I definitely want the base hunt value defined by the pot, as species of fish, wind conditions etc will demand different rpm of the motor.

Thats a big help, I will try and fit this into my coding!

Hello!

So i have been power studying, its a lot of info and very intriguing, but hoping you all can help steer me in the right direction t speed up my learning in regards to my specific project.

I am starting to understand the basic functions i want to accomplish, but the "ifs, ands, elses " etc are still overwhelming to get it all together.

So hopefully we can tackle each portion separately and start to mash them together?

So here is my code that is giving me my 90 degree movement commanded by the pot.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
	myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
	val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
	val = map(val, 0, 1023, 0, 90);     // scale it to use it with the servo (value between 0 and 90)
	myservo.write(val);                  // sets the servo position according to the scaled value
	delay(15);                           // waits for the servo to get there
}

Now, to focus on my my priority function would be the "Hunt", this is where i get confused, integration into existing code.

This is sort of how i see it looking...

myservowrite(val) //sets servo to potpin value
if ((no idea best way to do)switchHuntHigh
delay(60000) //time spent at pot position
myservowrite(val+20%) //elevated servo/engine rpm
delay(8000) // time spent at elevated servo setting

so I guess, what im asking is, what would be the recommended format for watching the hunt switch, and how do i get in and out of the IF functions.

As an example, here is my functioning hunt high feature code.

Its cylcles my servo exacly as expected, I just need to know how to switch form constant speed to this.

And now edited to include my hunt high toggle switch.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
int inPin=2; // number of the input pin (switch)
int reading; // the current reading from the input pin
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup() {
	myservo.attach(9);  // attaches the servo on pin 9 to the servo object
	pinMode(inPin, INPUT);
}

void loop() {
	val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
	val = map(val, 0, 1023, 0, 90);     // scale it to use it with the servo (value between 0 and 90)
	myservo.write(val);
	delay(10000);
	myservo.write(val*1.20);
	delay(3000);
}

I shortened the times to give me my proof of theroy but is what i want. Once I get my code written, I want to expand the 3 variables, (time at low speed, time at high speed and percentage of increase) with 3 more pots for on the fly adjustments.

Can someone please help me use the switch to switch from constant POT speed to hunting from Pot speed and 20% elevated speed. And then back to constant speed when toggle is turned off.

I suggest you take this one step at a time. You have learned how to read the potentiometer, and you have learned how to set the servo to a desired position, good!

Now have a look at how to read your switch or button (which one is it??) properly. I see you have added time and debounce variables, but haven't put them to use yet. Start of simple, maybe just turning the onboard LED on and off, there are plenty of resources out there.

You have to gather the ingredients before you start cooking. Otherwise, you end up with gibberish code. For example delay(60000) is a VERY bad idea, but I think you will realise this yourself once you start implementing the button.

Before you try to integrate the two programs you need to get the switch working in the "hunt" program.

What sort of switch do you have? As I mentioned earlier, if you have a latching push-button or a toggle switch the logic will be simpler. If you have a momentary contact push-button then you need to write code to toggle a variable when the switch is pressed.

And, sorry to be the bearer of bad news, but if you use delay()s in your program they will make the program unresponsive - the Arduino cannot do anything else during a delay(). For example, you could not cancel the "hunt" mode before it completed. If you need a responsive program have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

I know it means extra learning but incorporating millis() when the program is being created will be a lot simpler than re-writing a program to remove delay()s from it.

...R

Hey friend,

I have done that, I've been through the pin13 tutorial and controlled the onboard led.

I've been powering through tutorials, and enjoying all of it. I plan to continue to do so, however I also want to implement my learning sooner than later.

The one thing at a time approach is what I'm attempting. I made the switch run the LED, i made the pot run the servo as needed. I made a code performing my exact need for hunt feature.

Each individual step is a great learning curve, but immediately becomes overwhelming trying to mash it together.

Robin2,

Don't be sorry, this is my point. Im looking for some guidance, being told to go study is pretty counterproductive and of no help to anyone. Given a basic theory of how i can execute this and areas of focus to learn is needed, otherwise I need to study every area/aspect of code and decide what will work best based on trial and error and system knowledge, this is literally years of study.

So that said, thank you for the areas to focus on. Without it this is near an impossible task.

As for hardware, I have nothing yet. I'm in design phase, I will utilize the hardware required by the final rough design. At this point, judging by the videos I've watched and everyone suggestions a toggle will be easiest.

I'll keep slugging through what i can find on the net.

Thanks.

bobhodder:
The one thing at a time approach is what I'm attempting. I made the switch run the LED, i made the pot run the servo as needed. I made a code performing my exact need for hunt feature.

Good! Happy to hear that. If you post the sketch here, we can see how to further improve it

Sorry, is a sketch not what i posted earlier?

The program as a whole?

Forgive my ignorance.

bobhodder:
Sorry, is a sketch not what i posted earlier?

The program as a whole?

Forgive my ignorance.

Please show us the code where you had the pot ánd the button working

Oh, I gotcha.

That one i never nailed down. I've had a button running a light.

And a pot running a servo, never both at the same time. Multiple functions immediately go over my head, i only learned the first steps yesterday.

I started reading up on millis and cant grasp that either, lol.

I've started watching Paul McWhorters series of videos. and going through those, but again its soooo much information, I'm about a 1/3 way through but find he is explaining things so one can understand it.

I feel I'm simply over my head in this project, but I'm not one for accepting defeat.

Post what you have for the pot and the servo. Given that, it should be fairly simple to add the code that turns a light on and off. Once you have that working it's not much of a step to get your idle/resume function running.

Hunting mode is harder. If you want it to repeat the hunting cycle until you press the hunting button again, you will need to get your head around millis.

Well, I feel silly.

I searched what I thought was pretty extensively. Got home after a long graveyard shift, laying in bed at 6:00 am and gave it one more little google, first hit...

https://create.arduino.cc/projecthub/TTSquared/outboard-motor-throttle-controller-4edfd5

I loaded the ino into my simulator, ran the pin locals and boom, functions flawless. Looking through the code, there's no way I could of done this on my own.

Credits and thanks to Author, Terry Towle.

Thanks for the help! I will acquire the hardware and start putting it together. hopefully it works as well in practice as it did in sim.