Make 3 micro-switches operate 'non momentary'

Hi,

I am writing some code for motor control using an L298N board and an UNO(clone)

The code mainly works and changes the direction and pauses the motor correctly as the switches are pressed. BUT the mechanism for operating the roller micro-switches will be a notch in a rotating disc (on the motor from the L298 board) so currently when the switches are pressed they are only released when the notch comes round again. I think I need the HIGH/LOW state of each micro-switch to toggle somehow. (I have tried some copied boolean statements, but not managed to get them to work)

Sorry if this is a bit unclear, but hopefully the code will make it clearer:

// setting up of pin numbers: int microswitch1 = A0; // 9 oclock position switch int microswitch2 = A1; // 12 oclock position switch int microswitch3 = A2; // 3 oclock position switch int IN1 = 8; // motor1 controller output pin IN1 int IN2 = 9; // motor1 controller output pin IN2 int SP1 = 10; // motor1 speed control pin

void setup() { // initialise the switch pins as input: pinMode(microswitch1, INPUT_PULLUP); pinMode(microswitch2, INPUT_PULLUP); pinMode(microswitch3, INPUT_PULLUP);

// initialise the motor controller pins as output ; pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(SP1, OUTPUT);

}

void loop() {

//if Microswitch 1 is activated:( but what is really needed is; // "If microswitch 1 (analogue pin 1) changes state")

if (digitalRead(microswitch1) == LOW){ digitalWrite(IN1,LOW); digitalWrite(IN2, LOW); //set the motor to OFF

analogWrite(SP1,150); //motorspeed delay(5000); //wait for 5 seconds digitalWrite(IN1,LOW); digitalWrite(IN2, HIGH); //set the motor to clockwise

} else if(digitalRead(microswitch2)==LOW){ analogWrite(SP1,0); //stop motor delay(5000); //stay at stop speed for 5 seconds analogWrite(SP1,150); //resume motorspeed } else if(digitalRead(microswitch3)== LOW){ analogWrite(SP1,0); //stop motor delay(5000); //wait for 5 seconds digitalWrite(IN1,HIGH); digitalWrite(IN2, LOW); //set the motor to anti-clockwise analogWrite(SP1,255); //motorspeed

}

}

When the switches are pressed and held at the moment, the 5 second delay kicks in after the switch is released.

As you may guess, I am new to programming! Any help gratefully received.

Thanks

Look at the StateChangeDetection example in the IDE

can someone tell me why is don't work when I add long unsigned to" long unsigned currentmillis = millis(); " at start of main loop . it works perfect if I take long unsigned out but it needs it for roll over.

unsigned long currentmillis=0; unsigned long buttonmillis=0; unsigned long previousmillis=0;

define led 8

define D4 4

int button=0; int buttonTime=200;

void setup() { Serial.begin(115200); digitalWrite(D4,INPUT_PULLUP);

}

void loop() { long unsigned currentmillis = millis();

Button ();

digitalWrite(led,button);

} //end of program

int Button () { if (digitalRead(D4)==HIGH) buttonmillis = millis();

if ((unsigned long)(currentmillis-buttonmillis)>=(buttonTime)) { Serial.println(currentmillis-buttonmillis);

button=1;

; Serial.println(button); Serial.println(digitalRead(D4));

}else { button=0; } return button; }

can someone tell me why is don't work

Maybe. There's a clue in the fact that the type follows the modifier, as in your first declarations. The modifier does not follow the type. That is, it's unsigned long, not long unsigned.

The input mode of the button pin is INPUT_PULLUP so when not pressed it will be HIGH. I assume that you have it wired to go LOW when pressed, or have you ?

In the Button() function you have

  if (digitalRead(D4) == HIGH)
    buttonmillis = millis();

so buttonmillis will be set to millis() only when the button is not pressed. Is that what you meant to do ?

What is it that you are trying to do ?

Did you look at the StateChangeDetection example in the IDE ?