Sunday, December 7, 2014

ETEC 597: Maker Movement - Week 6

We made it!  After several weeks of tinkering and making, we are finally in the home stretch.  I, for one, have enjoyed working on these projects and coming up with solutions to each week's Maker Challenge.  This week's challenge was a culmination of outputs and inputs with our circuit having to consist of an electric motor connected to a sensor.  I chose to work with the light sensor rather than the thermal sensor because it is easier to make fast changes to light intensity when testing than making fast changes in temperature.  This decision worked out well once I was able to adjust the code and make the motor respond to changes in the photo resistor instantaneously.  The first code that I am posting is the original code for the motor control; the second set of code is the working copy of the code with the photo resistor.

Code
// Motor Controller Code

const int motorPin = 9;

void setup()
{
  // Set up the motor pin to be an output:

  pinMode(motorPin, OUTPUT);

  // Set up the serial port:

  Serial.begin(9600);
}


void loop()
{

  // motorOnThenOff();
  // motorOnThenOffWithSpeed();
  // motorAcceleration();
  // serialSpeed();
}


void motorOnThenOff()
{
  int onTime = 3000;  // milliseconds to turn the motor on
  int offTime = 3000; // milliseconds to turn the motor off
  
  digitalWrite(motorPin, HIGH); // turn the motor on (full speed)
  delay(onTime);                // delay for onTime milliseconds
  digitalWrite(motorPin, LOW);  // turn the motor off
  delay(offTime);               // delay for offTime milliseconds
}

void motorOnThenOffWithSpeed()
{
  int Speed1 = 200;  // between 0 (stopped) and 255 (full speed)
  int Time1 = 3000;  // milliseconds for speed 1
  
  int Speed2 = 50;   // between 0 (stopped) and 255 (full speed)
  int Time2 = 3000;  // milliseconds to turn the motor off
  
  analogWrite(motorPin, Speed1);  // turns the motor On
  delay(Time1);                   // delay for onTime milliseconds
  analogWrite(motorPin, Speed2);  // turns the motor Off
  delay(Time2);                   // delay for offTime milliseconds
}

void motorAcceleration()
{
  int speed;
  int delayTime = 20; // milliseconds between each speed step
  
  // accelerate the motor

  for(speed = 0; speed <= 255; speed++)
  {
    analogWrite(motorPin,speed); // set the new speed
    delay(delayTime);           // delay between speed steps
  }
  
  // decelerate the motor

  for(speed = 255; speed >= 0; speed--)
  {
    analogWrite(motorPin,speed); // set the new speed
    delay(delayTime);           // delay between speed steps
  }
}

void serialSpeed()
{
  int speed;
  
  Serial.println("Type a speed (0-255) into the box above,");
  Serial.println("then click [send] or press [return]");
  Serial.println();  // Print a blank line

  // In order to type out the above message only once,
  // we'll run the rest of this function in an infinite loop:

  while(true)  // "true" is always true, so this will loop forever.
  {
    // First we check to see if incoming data is available:
  
    while (Serial.available() > 0)
    {
      // If it is, we'll use parseInt() to pull out any numbers:
      
      speed = Serial.parseInt();
  
      // Because analogWrite() only works with numbers from
      // 0 to 255, we'll be sure the input is in that range:
  
      speed = constrain(speed, 0, 255);
      
      // We'll print out a message to let you know that the
      // number was received:
      
      Serial.print("Setting speed to ");
      Serial.println(speed);
  
      // And finally, we'll set the speed of the motor!
      
      analogWrite(motorPin, speed);
    }
  }
}

Picture of the Circuit without the Photo Resistor















Code with the Photo Resistor
// Motor Controller Code

const int sensorPin = 0;

const int motorPin = 9;

int lightLevel, high = 0, low = 1023;

void setup()
{
  // Set up the motor pin to be an output:

  pinMode(motorPin, OUTPUT);

  // Set up the serial port:

  Serial.begin(9600);
}


void loop()
{
  int lightSensor = analogRead(sensorPin/4);
  
  // motorOnThenOff();
  // motorOnThenOffWithSpeed();
  // motorAcceleration();
  // serialSpeed();
  motorSpeedwithLight();
}


void motorOnThenOff()
{
  int onTime = 3000;  // milliseconds to turn the motor on
  int offTime = 3000; // milliseconds to turn the motor off
  
  digitalWrite(motorPin, HIGH); // turn the motor on (full speed)
  delay(onTime);                // delay for onTime milliseconds
  digitalWrite(motorPin, LOW);  // turn the motor off
  delay(offTime);               // delay for offTime milliseconds
}

void motorOnThenOffWithSpeed()
{
  int Speed1 = 200;  // between 0 (stopped) and 255 (full speed)
  int Time1 = 3000;  // milliseconds for speed 1
  
  int Speed2 = 50;   // between 0 (stopped) and 255 (full speed)
  int Time2 = 3000;  // milliseconds to turn the motor off
  
  analogWrite(motorPin, Speed1);  // turns the motor On
  delay(Time1);                   // delay for onTime milliseconds
  analogWrite(motorPin, Speed2);  // turns the motor Off
  delay(Time2);                   // delay for offTime milliseconds
}

void motorAcceleration()
{
  int speed;
  int delayTime = 20; // milliseconds between each speed step
  
  // accelerate the motor

  for(speed = 0; speed <= 255; speed++)
  {
    analogWrite(motorPin,speed); // set the new speed
    delay(delayTime);           // delay between speed steps
  }
  
  // decelerate the motor

  for(speed = 255; speed >= 0; speed--)
  {
    analogWrite(motorPin,speed); // set the new speed
    delay(delayTime);           // delay between speed steps
  }
}

void motorSpeedwithLight()
{
  int speed;
  int lightSensor = analogRead(sensorPin);
  
    while(true)
    {
    speed = lightSensor/4; //Limits motor speed to 255
    analogWrite(motorPin, speed);
   }
}

Picture of the Circuit with the Photo Resistor



















Schematic Diagram



















Video of Working Circuit


Extension
Photo resistors are used in many devices around our homes and workplaces.  I recently installed a set of motion-sensitive security lights and the photo resistor made me think of those devices.  The photo resistors in the security light respond to changes in the amount of light that they detect; if this change is too drastic, then they send a signal to the controller to switch on the lights.

Although the photo resistor in a security light isn't directly related to the electric motor in this circuit, there are some similar analogues in our world.  Backup sensors on our can work by detecting distance or work by detecting light; those that detect light and stop the car are working in a similar way to the one in my circuit.  The photo resistor and motor circuit could also be used for automatic blinds in your house; if the sun is beaming too brightly, the motor can automatically lower the blinds in your window.

No comments:

Post a Comment