Switched Reluctance motor program works reversed...

int thermostat = A2;
int coil1 = 9;
int coil2 = 10;
int coil3 = 11;
int switchingtime;
int i;



void setup(){
pinMode(thermostat, INPUT);
pinMode(coil1, OUTPUT);
pinMode(coil2, OUTPUT);
pinMode(coil3, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
delay(switchingtime);
switchingtime = 1000;
}
void loop(){
    while (digitalRead(thermostat)== HIGH){
    digitalWrite(LED_BUILTIN, LOW);
    digitalWrite(coil1, HIGH);
    delay(switchingtime);
    digitalWrite(coil1,LOW);
    delay(switchingtime);
    digitalWrite(coil2, HIGH);
    delay(switchingtime);
    digitalWrite(coil2,LOW);
    delay(switchingtime);
    digitalWrite(coil3, HIGH);
    delay(switchingtime);
    digitalWrite(coil3,LOW);
    delay(switchingtime);
    return;
    }
  {
  digitalWrite(LED_BUILTIN, HIGH);
  return;
  }
  
}

I'm trying to use it to drive a 12v fridge compressor i'm working on.
i plan to use a switched reluctance motor turning on and off it's coils attaching tips 120 to the 9,10, and 11 pins, wich i wish to turn on and off in order.

i have a push button acting as thermostat on pin A2. A real fridge thermostat is about to get fit there whenever i be able to build the rest of the hardware.

i have 3 leds representing coil1, coil2, and coil3.

It does work, but it does in a reverse manner.

While button is unpressed, LED_BUILTING should stay lit.
Whenever i depress button, pins 9,10 and 11 should start counting.

What did i do wrong here? I'm on arduino uno attached to this gnu/linux pc...

Your Switch has one side connected to A2, what is the other side connected too, +5V or GND?

Connect a 10k resistor from A1 to GND,

JCA34F, I suspect you meant A2. Still, why not use INPUT_PULLUP on A2’s pin mode and then connect the other side of the switch to GND? Then change the while to LOW. That way, with the switch open, A2 will be guaranteed to be high and when pressed, it will be low.

PickyBiker:
Your Switch has one side connected to A2, what is the other side connected too, +5V or GND?

Hi! it's between A2 and GND.

PickyBiker:
JCA34F, I suspect you meant A2. Still, why not use INPUT_PULLUP on A2's pin mode and then connect the other side of the switch to GND? Then change the while to LOW. That way, with the switch open, A2 will be guaranteed to be high and when pressed, it will be low.

I just did what you suggested and it works as intended!
just let me thank you for your support!

I have this source, it works.

I just wish to add a safety measure. i need to make it to wait 120 seconds (two minutes) after depressing the "thermostat" switch , so it doesn't get damaged.

i tried to add a "delay" before doing anything but it just gets stuck. how can i do it?
Can i get LED_BUILTIN blinking after thermostat on / button so there's indication that motor is about to start?
I believe that a FOR line could be added, but it's my first Arduino project ever..

Please, see the successful source.. is this..


int thermostat = A1;
int coil1 = 9;
int coil2 = 10;
int coil3 = 11;
int switchingtime;
int switchinglenght;
int pressurerelieftime;
int i;



void setup(){
pinMode(thermostat, INPUT_PULLUP);
pinMode(coil1, OUTPUT);
pinMode(coil2, OUTPUT);
pinMode(coil3, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
delay(switchingtime);
switchingtime = 0005;
switchinglenght = 0005;
pressurerelieftime = 10000;
}
void loop(){
    while (digitalRead(thermostat)== LOW){
    digitalWrite(LED_BUILTIN, LOW);
    digitalWrite(coil1, HIGH);
    delay(switchinglenght);
    digitalWrite(coil1,LOW);
    delay(switchingtime);
    digitalWrite(coil2, HIGH);
    delay(switchinglenght);
    digitalWrite(coil2,LOW);
    delay(switchingtime);
    digitalWrite(coil3, HIGH);
    delay(switchinglenght);
    digitalWrite(coil3,LOW);
    delay(switchingtime);
    return;
    }
  {  
  digitalWrite(LED_BUILTIN, HIGH);
  return;
  }
  
}

To make a delay that does not block, start with this post.

I'm trying to drive a 3 coil switched reluctance motor.
I do have to turn on and off, in sequence, coil 1, coil 2 and coil 3.
I wish to use pins A1, A2, A3 and A4, sending them to ground by means of a resistor, to select the speed of it.
How can i do it?

Which controller do You use?
What current does each motor coil need?
Link to that motor, please....

You seem to be missing one important piece of the motor: "the switched-reluctance motor uses an electronic position sensor to determine the angle of the rotor shaft".

Paul

Trendset:
I’m trying to drive a 3 coil switched reluctance motor.
I do have to turn on and off, in sequence, coil 1, coil 2 and coil 3.
I wish to use pins A1, A2, A3 and A4, sending them to ground by means of a resistor, to select the speed of it.

You referred in your other posting, to driving the coils with IIRC, TIP120s (not a good choice!). What does this speed selection do, and why do you refer to analog (input) pins?

Do be careful anyway about using A4, since it is what you need to use for I2C.

@Trendset

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.