[solved]For function whit bigger steps than i++

Goy my code working only one thing i want to have is working

 //situation1
 for (int i = 0; i <= 255; i++) { 
for (i = testpwm1; i <= turn2; i+=settime)     // what i have done in my code


//situation2
for (int i = 255; i <= 0; i--) { 
for (i = testpwm1; i >= turn2; i-=settime)//what i have done in my code

But than instead of i++ diffrent value
whit i+=20 i get steps of 20
so it is faster. but when 255 is 15 it can't jump anymore to 0 but i like to have it at 0
in for - Arduino Reference
is say something more but tried it and the program does not undestand it

in the code i use variable for the numbers. whit settime i can get it to work whit stepts that i can set whit potentiunmeter and mapped between 0 and 50.
but it needs to go to turn2 value

is there a way to do this correctly?

that' will get you NO iteration as 255 is not <= 0

May be you wanted

for (int i = 255; i >= 0; i--) { 

sorry my misstake
i copied the line from the arduino site and only changed the last part ++ to --
didn't check the rest of it.

At this moment i am thinking of using if else after the for line is done.

for (i = testpwm1; i <= turn2; i+=settime)
if (i-turn2<settime) i=0;

and

for (i = testpwm1; i >= turn2; i-=settime)
if (turn2-i<settime) i=0;

only will that work for both situations

messing around with your index (i) within the for() {} loop is usually not a great idea
what are you really trying to achieve - may be a while() would be better?

It looks like @trucker0werner wants to use step sizes in the for loop that result in the end values not being exactly 0 but wants to end up with a value of 0 anyway

This if for softstart motor or softstop

turn2 and settime are both controlled whit potentionmeter.
motor runs at 200
speed (turn2) is set to 100
and the steps it needs to take is 15.

whit i++ and i-- i get steps of 1 and it works correclty, but it takes to long .
to speed it using settime in it works.
but in the example aboven it would go down to 110 but not 100.
for that i am looking for solution or a diffrent way to to the same thing.

ah OK

try something like this - totally untested, typed here:

void doSomething(long v) {
  Serial.println(v); // do somehting
}

void doSomethingBetween(long startValue, long endValue, long stepSize) {
  long currentValue = startValue;
  while (true) {
    if (endValue > startValue) { // increase
      if (currentValue <= endValue) {
        doSomething(currentValue); // do somehting
        if (currentValue != endValue)
          if (currentValue + stepSize > endValue)
            currentValue = endValue;
          else
            currentValue += stepSize;
        else break;
      } else break;
    } else { // decrease
      if (currentValue >= endValue) {
        doSomething(currentValue); // do somehting
        if (currentValue != endValue)
          if (currentValue - stepSize < endValue)
            currentValue = endValue;
          else
            currentValue -= stepSize;
        else break;
      } else break;
    }
  }
  Serial.println("---- DONE ----");
}

void setup() {
  Serial.begin(115200);
  Serial.println();

  doSomethingBetween(10, 80, 12); // go fom 10 to 80 in steps of 12
  doSomethingBetween(80, 10, 12); // go fom 80 to 10 in steps of 12
}


void loop() {}

(don't call it with a negative or null stepSize or it will misbehave - probably something to test in the function)

run that code with the Serial monitor open at 115200 bauds

that is a long code
atleast to long for the room i have left.
checked it and empty sketch is 9bytes and this code is 202bytes.

But forgot to say that i need 1kb atleast for the ssd1306 oled screen. and so i have not alot room left
have now 1475bytes of room but under 1200bytes left and ssd1306 does not have enough room.

so that is why i am trying to get a diffrent short way of doing it

[solution found]
i found the solution i was looking for
tested it and seem to be working

      for (i = testpwm2; i >= turn2; i-=settime) { // slow down left-----
        settime2=turn2-i;
        if (settime2<settime) i=turn2;

the orther way around

      for (i = testpwm2; i <= turn2; i+=settime) { //speed up left------
        settime2=turn2-i;
        if (settime2<settime) i=turn2;

and for full speed down 
    for (i = testpwm1; i >= 0; i-=settime) { // slow down right-----
      if (i<settime) i=0;

testpwm1 and testpwm2 are left and right
all 3 are setting i=turn2 if the settime is bigger than the settime2.
settime2 is calculation of turn minus what the "i" is in the "for"structure.

the names in the sketch are not wat it needs to be, didn't want to spend time on the correct name for what it is doing

I don’t think my code above eats up so much memory if you remove all the text printing that was there for the example. That's where the SRAM went.

if you compile this:

void doSomethingBetween(long startValue, long endValue, long stepSize) {
  long currentValue = startValue;
  while (true) {
    if (endValue > startValue) { // increase
      if (currentValue <= endValue) {
        PINB = 0b100000; // do something

        if (currentValue != endValue)
          if (currentValue + stepSize > endValue)
            currentValue = endValue;
          else
            currentValue += stepSize;
        else break;
      } else break;
    } else { // decrease
      if (currentValue >= endValue) {
        PINB = 0b100000; // do something

        if (currentValue != endValue)
          if (currentValue - stepSize < endValue)
            currentValue = endValue;
          else
            currentValue -= stepSize;
        else break;
      } else break;
    }
  }

}

void setup() {
  doSomethingBetween(10, 80, 12); // go fom 10 to 80 in steps of 12
  doSomethingBetween(80, 10, 12); // go fom 80 to 10 in steps of 12
}

void loop() {}

it's 474 bytes of flash and 9 bytes of SRAM on a UNO

if you compile an empty code where the function has none of the business logic to iterate

void doSomethingBetween(long startValue, long endValue, long stepSize) {
  PINB = 0b100000; // do something
}

void setup() {
  doSomethingBetween(10, 80, 12); // go fom 10 to 80 in steps of 12
  doSomethingBetween(80, 10, 12); // go fom 80 to 10 in steps of 12
}

void loop() {
}

you get 450 bytes of flash and 9 bytes of SRAM on a UNO.

so the delta is 24 bytes of flash (program) memory for the business logic (the PINB = 0b100000; // do something is there so that the compiler does not throw everything away)

but if you found something you are happy with - go for it.

You probably spent more time writing this than doing the search and replace that would make your code more readable :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.