Switch case and motor control

I happen to have a motor laying around and wanted to try some work with the switch case variables. I have it working with the sensor, it reads brighest and all that. Really just loaded the example and added a pin for the motor and a analog write line for pin 1.

I know the motor works but doesn't spin when on that case. The serial monitor prints the line though. Code and layout are below. The analogwrite line has been set to other value, like 1023, tried digitalwrite at HIGH too.

const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 600;    // sensor maximum, discovered through experiment
int motorPin = 1;

void setup() {
  // initialize serial communication:
 Serial.begin(9600); 
 pinMode(motorPin, OUTPUT);
}

void loop() {
  
  pinMode(motorPin, OUTPUT);
  // read the sensor:
  int sensorReading = analogRead(A0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the 
  // range value:
  switch (range) {
  case 0:    // your hand is on the sensor
    Serial.println("dark");
    break;
  case 1:    // your hand is close to the sensor
    Serial.println("dim");
    break;
  case 2:    // your hand is a few inches from the sensor
    Serial.println("medium");
    break;
  case 3:    // your hand is nowhere near the sensor
    analogWrite(motorPin, sensorMax);
    Serial.println("bright");
    break;
  } 
  delay(1);        // delay in between reads for stability
}

https://www.dropbox.com/s/elbf9ratl2nu4m7/swtichcasemotor.png

Try to avoid using pins 0 & 1 for your motor, as they're used by the serial interface.

Where in your case statements are you telling the motor to turn?

pinMode(motorPin, OUTPUT); is not needed in loop() as you already have it in setup().

The pins of the Arduino cannot be used to control a motor as they cannot provide the current required. You need at least a transistor, if not a motor shield. Look in the play ground for examples, and products for a motor shield.

Mark

PS you may have harmed your Arduino!

M

holmes4:
The pins of the Arduino cannot be used to control a motor as they cannot provide the current required. You need at least a transistor, if not a motor shield. Look in the play ground for examples, and products for a motor shield.

Mark

PS you may have harmed your Arduino!

M

What makes you think I may have harmed the arduino?

Drawing to much current from a pin may damage it.

Mark

… especially when applied to an inductive load.