hello sachleen try this code it working I edited from your code
// This will store the last known state of the button
const int buttonPin = A15; // change as per your button attached.
const int motorPin = 12; //
const int ledPin = 13;
int x = 1;
// variables will change:
int buttonState = 0;
int oldButtonState = LOW;
void setup() {
// initialize the LED pin as an output:
pinMode(motorPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() { // Get the current state of the button int newButtonState = digitalRead(buttonPin);
// Has the button gone high since we last read it? if (newButtonState == HIGH && oldButtonState == LOW) {
// This will store the last known state of the button
const int buttonPin = A15; // change as per your button attached.
const int motorPin = 12; //
const int ledPin = 13;
int x = 1;
// variables will change:
int buttonState = 0;
int oldButtonState = LOW;
void setup() {
// initialize the LED pin as an output:
pinMode(motorPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
// Get the current state of the button
int newButtonState = digitalRead(buttonPin);
// Has the button gone high since we last read it?
if (newButtonState == HIGH && oldButtonState == LOW) {
if (x == 0) {
// Toggle on
digitalWrite(ledPin, HIGH);
x = 1;
} else {
// Toggle off
digitalWrite(ledPin, LOW);
x = 0;
}
}
// Store the button's state so we can tell if it's changed next time round
oldButtonState = newButtonState;
}
}
// Store the button's state so we can tell if it's changed next time round oldButtonState = newButtonState; }