Motor CC turning (Project 9)

Hello, I bought the starter kit of Arduino (the kit with the book which has 15 projects) and I tried to do the 9th but my motor is still turning when I am pressing my button, I did a simulation ; Code:
c++
const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;

void setup() {
Serial.begin(9600);
pinMode(switchPin, OUTPUT);
pinMode(motorPin, OUTPUT);
}

void loop() {
switchState = digitalRead(switchPin);

Serial.println(switchState);

if (switchState = HIGH) {
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
}
}
c++
const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;

void setup() {
Serial.begin(9600);
pinMode(switchPin, OUTPUT);
pinMode(motorPin, OUTPUT);
}

void loop() {
switchState = digitalRead(switchPin);

Serial.println(switchState);

if (switchState = HIGH) {
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
}
}
No description
28 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Program
ProgramOP3y ago
And on B what I do then? On the book it's like this so what I have to change with my transistor. My transistor is mosfet in my kit.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
AnonEngineering
it gets clearer, if it's an N channel MOSFET the pinout is likely Gate Drain Source... but the sim won't know that...
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Program
ProgramOP3y ago
I have this one.
No description
No description
No description
Program
ProgramOP3y ago
@Deleted User
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Program
ProgramOP3y ago
c++
const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;

void setup() {
Serial.begin(9600);
pinMode(switchPin, OUTPUT);
pinMode(motorPin, OUTPUT);
}

void loop() {
switchState = digitalRead(switchPin);

Serial.println(switchState);

if (switchState = HIGH) {
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
}
}
c++
const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;

void setup() {
Serial.begin(9600);
pinMode(switchPin, OUTPUT);
pinMode(motorPin, OUTPUT);
}

void loop() {
switchState = digitalRead(switchPin);

Serial.println(switchState);

if (switchState = HIGH) {
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
}
}
9 -> to the first born of the transistor (first img). 2 -> the button.
No description
Program
ProgramOP3y ago
No description
Program
ProgramOP3y ago
@Deleted User :>. No idea?
Program
ProgramOP3y ago
I have the same circuit :
No description
Program
ProgramOP3y ago
But it does not, the motor is not stopping when I pressed or not on the button. But I have info about the button so it works. But just the motor is not listening.
c++
const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;

void setup() {
Serial.begin(9600);
pinMode(switchPin, OUTPUT);
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, LOW);
}

void loop() {
switchState = digitalRead(switchPin);
if (switchState = HIGH) {
Serial.println(digitalRead(motorPin));
digitalWrite(motorPin, HIGH);
Serial.println(digitalRead(motorPin));
} else {
Serial.println(digitalRead(motorPin));
digitalWrite(motorPin, LOW);
Serial.println(digitalRead(motorPin));
}
}
c++
const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;

void setup() {
Serial.begin(9600);
pinMode(switchPin, OUTPUT);
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, LOW);
}

void loop() {
switchState = digitalRead(switchPin);
if (switchState = HIGH) {
Serial.println(digitalRead(motorPin));
digitalWrite(motorPin, HIGH);
Serial.println(digitalRead(motorPin));
} else {
Serial.println(digitalRead(motorPin));
digitalWrite(motorPin, LOW);
Serial.println(digitalRead(motorPin));
}
}
I have always "1".
AnonEngineering
the switchPin is an INPUT
Program
ProgramOP3y ago
Did not fix :(. @AnonEngineering
AnonEngineering
need a lot more detail, how is the switch wired, for one and what is driving the motor, a transistor hopefully?
Program
ProgramOP3y ago
Yes? Look at the circuit?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Program
ProgramOP3y ago
No, but there you have to stop making fun of the world. If you are not able to look at a circuit, there is a problem, EVERYTHING IS ON IT, I GAVE EVERYTHING.
Program
ProgramOP3y ago
Wtf??? Why it's not "=="... Mb I will try.
AnonEngineering
== compares two values, = sets a value to be equal to
Program
ProgramOP3y ago
Idk why I did not set "=="... Thanks. SO MUCH. I tought it was a problem about my motor :(. Now one thing :
c++
const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;

int enableMotor;

void setup() {
Serial.begin(9600);
pinMode(switchPin, INPUT);
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, LOW);
}

void loop() {
switchState = digitalRead(switchPin);
if (switchState == HIGH) {
enableMotor = 1;
} else {
enableMotor = 0;
}

Serial.println(enableMotor);

if (enableMotor == 1) {
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
}
}
c++
const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;

int enableMotor;

void setup() {
Serial.begin(9600);
pinMode(switchPin, INPUT);
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, LOW);
}

void loop() {
switchState = digitalRead(switchPin);
if (switchState == HIGH) {
enableMotor = 1;
} else {
enableMotor = 0;
}

Serial.println(enableMotor);

if (enableMotor == 1) {
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
}
}
I would like to do not have to press always on the button. Once I press -> motor turn until I press. If I press again it stops the motor. But I tried this, it does not work.
AnonEngineering
look at the StateChangeDetect example any progress?
Program
ProgramOP3y ago
I did not understand...
Program
ProgramOP3y ago
Ah thanks.
c++
const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;
int motorState = 0;
int oldSwitchState = 1;

void setup() {
Serial.begin(9600);
pinMode(switchPin, INPUT_PULLUP);
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, LOW);
}

void loop() {
switchState = digitalRead(switchPin);
if (switchState != oldSwitchState) {
if (switchState == LOW) {
Serial.println("Switch was pressed");
motorState = !motorState;
Serial.print("Motor state is ");
Serial.println(motorState? "true" : "false");
} else {
Serial.println("Switch was released\n");
}
delay(20);
}
oldSwitchState = switchState;

digitalWrite(motorPin, motorState);
}
c++
const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;
int motorState = 0;
int oldSwitchState = 1;

void setup() {
Serial.begin(9600);
pinMode(switchPin, INPUT_PULLUP);
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, LOW);
}

void loop() {
switchState = digitalRead(switchPin);
if (switchState != oldSwitchState) {
if (switchState == LOW) {
Serial.println("Switch was pressed");
motorState = !motorState;
Serial.print("Motor state is ");
Serial.println(motorState? "true" : "false");
} else {
Serial.println("Switch was released\n");
}
delay(20);
}
oldSwitchState = switchState;

digitalWrite(motorPin, motorState);
}
Alright I will study this.
AnonEngineering
feel free to ask if you get stuck

Did you find this page helpful?