for loop keeps repeating

Gday,

I am building a magnet motor (Joe Flynn 1995) to drive a generator this has 7 coils on the stator 2 magnets on the rotor.

The coils need to be powered briefly in turn (1,5,2,6,3,7,4) and I wish to control these pulses to give me 1500 rpm. I am controlling these pulses with an Arduino Pro Micro connected to Mosfet or IGBT switches.

The sketch attach shows that there are 2 loops : a for loops the start and build up the revs and a do…while loop to keep it running which it will do continuously.

The problem is that i have had little programming experience since the 80’s, so please make your advice as specific as possible

sketch_MagGen.ino (2.07 KB)

Exit your first loop when the RPM reaches 1500??? Isn’t that what you want?

Paul

thank you. Yes!

I have it running a test with leds and it just keeps running the for lop.

I don't think for() plays well with floats. See how it works with integers.

I have tried it with integers but they will work with the precision I was seeking but I will try the for() with integers and the do...while with floats.

Thanks for the reply.

no luck,

Changing for() to int didn't make an difference but I'll leave it that way.

Any more thoughts?

You never call loop2().

#include <Bridge.h>
#include <BridgeClient.h>
#include <BridgeServer.h>
#include <BridgeSSLClient.h>
#include <BridgeUdp.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>
#include <Arduino.h>

// MagGen
// June 13, 2017;  upgraded May 10, 2019; upgraded May29, 2019
//int delayTime = 100; //this is our initial time delay.

void setup()

{ //declare 2,10,16,14,15,18-21. as outputs
  pinMode(21, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(20, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(19, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(18, OUTPUT);
  pinMode(10, OUTPUT); //power switch
  pinMode(2, OUTPUT); //Startup indicator
}
float delayTime;

void loop() {
  //We are now setting our delayTIme
  for (float delayTime = 1000.0; delayTime >= 2.7; delayTime = delayTime*0.9)
  {
    digitalWrite(2, HIGH);
    digitalWrite(21, HIGH); delay(delayTime);
    digitalWrite(21, LOW);
    digitalWrite(15, HIGH); delay(delayTime);
    digitalWrite(15, LOW);
    digitalWrite(20, HIGH); delay(delayTime);
    digitalWrite(20, LOW);
    digitalWrite(14, HIGH); delay(delayTime);
    digitalWrite(14, LOW);
    digitalWrite(19, HIGH); delay(delayTime);
    digitalWrite(19, LOW);
    digitalWrite(16, HIGH); delay(delayTime);
    digitalWrite(16, LOW);
    digitalWrite(18, HIGH); delay(delayTime);
    digitalWrite(18, LOW);
  }
}
void loop2() {
  do {
    digitalWrite(2, LOW); // Turn 0ff LED, turn on power output switch
    digitalWrite(10, HIGH);
    digitalWrite(21, HIGH); delay(2.7);
    digitalWrite(21, LOW);
    digitalWrite(15, HIGH); delay(2.7);
    digitalWrite(15, LOW);
    digitalWrite(20, HIGH); delay(2.7);
    digitalWrite(20, LOW);
    digitalWrite(14, HIGH); delay(2.7);
    digitalWrite(14, LOW);
    digitalWrite(19, HIGH); delay(2.7);
    digitalWrite(19, LOW);
    digitalWrite(16, HIGH); delay(2.7);
    digitalWrite(16, LOW);
    digitalWrite(18, HIGH); delay(2.7);
    digitalWrite(18, LOW);
  }
  while (delay < 2);
}

"You never call loop2()."

I'm sorry, is that a question or a statement.

I tried a second void loop() but it wouldn't accept that. I saw example that used void loop2() and void loop3(), so I tried that and the programme allowed it. I have also tried it without a second void loop and that didn't do anything either.

I'm sorry, is that a question or a statement

It is a statement

Once the for loop in the loop() function ends the loop() function, including the for loop, will be repeated and the sequence will be repeated. The code has no way of running the code in the loop2() function.

If you want to run the code in loop2() after the for loop in loop() then just call it before the end of loop().

    delay(delayTime);
    digitalWrite(18, LOW);
  } //end of for loop
  loop2();
} //end of loop()

Note that for loops only work with ints and that delay() does not work with floats. If you want delays that are a fraction of a millisecond then look to the micros() function in the first instance

Alternatively put the for loop in setup() so that it runs once and the code in the loop2() function in the loop() function to run repeatedly

Thank-you UKHeliBob

I took your alternative of placing the 'for loop' within the 'void setup()' section. A few curly bracket adjustments and everything looks good on my LED tests.

Now to the tools to make the mechanical part work.

I would never been able to have sorted this out.

Regards Bill.

I’m glad you got it working.

The names of the functions give the game away as to what they do

setup() things that only happen once at the start of the program
loop() things that need to happen repeatedly

In addition, if you write your own functions then they need to be called explicitly t the time they are required to run

G’day again

This is still Mausgras but I had to re-register because I had cancelled my old email address.

Still! over the last few days I have ad a few senior moments I:

  1. failed to save my sketch
  2. soldered the working board upside down and in re-soldering I damaged the RAW connector which is essential.

So! I went back to my IDE to upload my sketch to another board, but I found that I didn’t have the latest sketch and so I tried to move the for loop to setup() as I had previously done.

But all I got was “unregistered-ID” and “initializer” messages. I have tried “{”, “;”, int statements and everything else I could find. (especially Code Problems, Compile Errors).

Can someone please Help

Regards Bill.

#include <Bridge.h>
#include <BridgeClient.h>
#include <BridgeServer.h>
#include <BridgeSSLClient.h>
#include <BridgeUdp.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>
#include <Arduino.h>

// MagGen
// June 13, 2017;  upgraded May 10, 2019; upgraded May29, 2019
int delayTime = 1000; //this is our initial time delay.


void setup()

{ //declare 2,10,16,14,15,18-21. as outputs
  pinMode(21, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(20, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(19, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(18, OUTPUT);
  pinMode(10, OUTPUT); //power switch
  pinMode(2, OUTPUT); //Startup indicator
}



int delayWrite;
{
//We are now setting our delayTIme
for (int delayTime = 1000; delayTime >= 2; delayTime = delayTime * 0.9)
{
  digitalWrite(2, HIGH);
  digitalWrite(21, HIGH); delay(delayTime);
  digitalWrite(21, LOW);
  digitalWrite(15, HIGH); delay(delayTime);
  digitalWrite(15, LOW);
  digitalWrite(20, HIGH); delay(delayTime);
  digitalWrite(20, LOW);
  digitalWrite(14, HIGH); delay(delayTime);
  digitalWrite(14, LOW);
  digitalWrite(19, HIGH); delay(delayTime);
  digitalWrite(19, LOW);
  digitalWrite(16, HIGH); delay(delayTime);
  digitalWrite(16, LOW);
  digitalWrite(18, HIGH); delay(delayTime);
  digitalWrite(18, LOW);
}//end of loop
)

void loop() {
  do {
    digitalWrite(2, LOW); // Turn 0ff LED, turn on power output switch
    digitalWrite(10, HIGH);
    digitalWrite(21, HIGH); delay(2.7);
    digitalWrite(21, LOW);
    digitalWrite(15, HIGH); delay(2.7);
    digitalWrite(15, LOW);
    digitalWrite(20, HIGH); delay(2.7);
    digitalWrite(20, LOW);
    digitalWrite(14, HIGH); delay(2.7);
    digitalWrite(14, LOW);
    digitalWrite(19, HIGH); delay(2.7);
    digitalWrite(19, LOW);
    digitalWrite(16, HIGH); delay(2.7);
    digitalWrite(16, LOW);
    digitalWrite(18, HIGH); delay(2.7);
    digitalWrite(18, LOW);
  }
  while (delay < 3);
}