trying to read value on PWM pin(Solved)

EDIT:
I changed my for loop into if statements and that perfectly resolved my issue. Just too bad nobody read my first sentence and assisted with that. Instead all I really got was that my code was wrong (aside from UKHeliBob who pointed out my huge mistake in trying to read a PWM value from a port). NO KIDDING! That's why I came here asking for assistance. I knew my code was wrong. This is 2 for 2 now where everything but my actual question was addressed. I understand now why so many people ask questions but never follow up and continue the thread to conclusion.
SOLUTION TO MY QUESTION:

  if (drctn == halt)
  {
    if (en > 0)  //whatever value the enable pulse is, if it is more than zero 
    {
      en = en - 1; // decrement value by 1 it until it is zero
    }
    analogWrite(enable, en); //write the decreasing PWM value to the port
  }

I am wanting to gradually stop and start motors on a bot. I am getting an error, "Expression cannot be used as a function" when compiling. I am trying to read the current state of pin 6, which is my PWM enable pin and slow down from that point. In the line, for (int i = en; i > 0; i--(20))
I don't want to set i to 255 because that may have it jumping to full throttle when it was only at half speed to start. I have made several changes to this as I am somewhat guessing the syntax because I can't find anything expressly doing what I want. Everything assigns "i" a value, where I want to assign it a value based on the current state of the pin. If you want more of the code I can give it but I think this is the relevant portions. Everything worked until I added the speed control part and used analogWrite(enable, i) instead of analogWrite(enable, 0).

Can I read pin 6 like this?

#define enable 6 //enable pin for motor driver
int halt;
int i = 0;
int en = 0;
int drctn = 0;

void setup()
{
  pinMode(enable, OUTPUT);
}


void loop()
{

  //all the things that determined drctn equaled halt

  if (drctn == "halt")
  {
    en = analogRead(enable);
    for (int i = (en); i > 0; i--(20))   //THIS is the line getting the error.  
    {
      analogWrite(enable, i);
    }
  }
}

Also unsure about (en) vs en in the for statement.
I didn't include everything else because there is a lot of IR sensor stuff and code determining I want to stop that doesn't seem relevant. I tried this part alone and get the same error as when compiling the entire thing.

i--(20) is not valid syntax. Maybe you meant to use -=

Exactly which pin do you mean by 6 ?
Is it digital pin 6 or analogue pin 6 ?

Either way something is wrong.
Digital pin 6 is not an analogue pin so using analogRead() with it makes no sense
Using analogRead() on analogue pin 6 makes no sense because its output will only ever be 0V or 5V unless you add some external circuitry. That's the way that PWM works

pert:
i--(20) is not valid syntax. Maybe you meant to use -=
-= - Arduino Reference
[/qre closeuote]
You we

pert:
i--(20) is not valid syntax. Maybe you meant to use -=
-= - Arduino Reference

You were close, and so was I. Turns out I needed
for (int i = (en); i > 0; i-20)
I've only been at this for about 2 weeks or so and some of the small things get me. In that time though I have managed to write a sketch that:
using an IR remote can select an autonomous mode that lights a warning LED and uses sonar to detect obstacles and automatically turns, then resumes, or, to select a remote controlled mode that uses the IR remote to move fwd, back, left, right and stop as well as operate a servo for turning the head. This question came up when I was trying to smooth out the starts and stops.

Next I think I'll add a light seeking behavior, then a sound seeking behavior to the autonomous mode. My only programming prior to 2 weeks ago was a class on machine language with the HC6811 about 20 years ago.

UKHeliBob:
Exactly which pin do you mean by 6 ?
Is it digital pin 6 or analogue pin 6 ?

Either way something is wrong.
Digital pin 6 is not an analogue pin so using analogRead() with it makes no sense
Using analogRead() on analogue pin 6 makes no sense because its output will only ever be 0V or 5V unless you add some external circuitry. That's the way that PWM works

DOH!
I've been up too long. Of course you are right. I don't need to read the pin because of course it will only be high or low. What I need to do is just take the current value of enable and incorporate it. This learning thing is fun but frustrating. I know how PWM works but for some reason I had it in my head that that value would be ON the port when it is actually IN the enable variable.
MORE COFFEE!!!!! LOL

I guess the next question is, can I set en = enable or will enable always be equal to the pin number since I set #define enable 6? Do I need to redefine enable as a variable in the void loop() or change the #define to something else?

JoeWillson:
You were close, and so was I. Turns out I needed
for (int i = (en); i > 0; i-20)

Really? That will just result in an endless loop. The equivalent (but much easier to understand) code is:

analogWrite(enable, en);
while(true);  // endless loop

pert:
Really? That will just result in an endless loop. The equivalent (but much easier to understand) code is:

analogWrite(enable, en);

while(true);  // endless loop

I don't understand... again...
How I think it works :slight_smile:
for (int i = (en); i > 0; i-20)
make I equal to the variable en
see if I is greater than 0
if it is greater than 0 subtract 20 from i

The next pass it sees I as 20 less that en and compares to see if it is still greater than 0
If so it subtracts 20 more from i
continue until i is no longer greater than 0, at which point it ignores the for statement.
In the examples I see for (int i = (en); i > 0; i--) which subtracts 1 from i each pass until it reaches 0.

No? Does the -= do what I am thinking the - does in this case? what is the difference between i-20 and i-=20?

No.

To learn how for loops work, and the difference between - and -=, write a simple sketch that just runs a for loop and prints the value of the variable to Serial Monitor.

pert:
No.

To learn how for loops work, and the difference between - and -=, write a simple sketch that just runs a for loop and prints the value of the variable to Serial Monitor.

I definitely will tomorrow.. err later today.
Thank you. I'll figure this out yet. I'lll just keep adding a little more complexity at a time. :slight_smile:

i--(20)

That is no a way to subtract 20 from the variable named 'i'.

This is the most specific way:

i = i - 20

And this is a shortcut for the same:

i -= 20
  if (drctn == "halt")

That is NOT the way to compare two integer variables. If you put the name of a variable in double-quotes it becomes a literal constant and no longer has anything to do with the variable.

What you probable want is:

  if (drctn == halt)

There is no provision to 'read the value' of a PWM output pin. Generally you would just store the value in a variable before you call analogWrite() so then you know what variable to look in to find the value you wrote.

johnwasser:

  if (drctn == "halt")

That is NOT the way to compare two integer variables. If you put the name of a variable in double-quotes it becomes a literal constant and no longer has anything to do with the variable.

What you probable want is:

  if (drctn == halt)

What I have works. I could have likely used a better declaration of halt.
It is based on a reading of the IR receiver.

if (results.value == 0xFF02FD)
  {
    drctn = "halt";
  }
if (results.value == 0xFF02FD)
  {
    drctn = "halt";
  }

This seems to make no sense. drctn is declared as an int

UKHeliBob:

if (results.value == 0xFF02FD)

{
    drctn = "halt";
  }



This seems to make no sense. drctn is declared as an int

That's one of the evil effects of >:( >:( >:( -fpermissive >:( >:( >:( .