Project 9 - Motorized Pinwheel

I have a problem with Project 9 - motorised pin wheel.

When I first plugged in the Arduino, the wheel started spinning even without any code in, but was not controlled by the switch. Then when I put the code in, the wheel doesn't spin anymore.

Here is my code

const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;

void setup () {
pinMode (motorPin, OUTPUT);
pinMode (switchPin, INPUT);
}

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

if (switchState == HIGH) {
digitalWrite (motorPin, HIGH);
}
else {
digitalWrite (motorPin, LOW);
}
}

--//--

I have tried the following to resolve the issue --
I tried changing the transistor
I tried changing the switch
I have tried erasing with the reset button and uploading.
Putting gaps between the void codes
I have changed the resistor

None of them work

--//--

Please see the attached image of my board.

Can anyone help please?

Thank you very.

Greatly appreciated.

Warmly,
Natasha

pinwheel.pdf (193 KB)

Please see the attached image of my board.

What did you attach it to?

I am assuming you are talking about the image of my board? The post has now been edited and you will be able to see what I did.

I think that you misplaced the switch by one row on your breadboard, so that is is effectively not connected at all.

Also take care to mount the switch in the right orientation. Two adjacent pins are always connected, so that it can happen that the switch appears closed all the time. If so, turn it by 90° to access pins from both connected pairs.

Thank you! That solves one problem. But the other problem I now have is that it doesn't work form batter supply only, it only works connected to my computer... Any thoughts? Thanks again! Warmly, Natasha

it doesn't work form batter supply only

How are you supplying the batter? Is it pancake batter?

That 9V battery is good for the battery manufacturer - highest consumer cost for least current. It is NOT good for you.

How are you connecting the battery to the Arduino? I can't tell from your photo.

It is connected black wire to positive and red wire to negative.

if I put it the other way round, as in the book, it does not work at all...