Coding

I anticipated that this would be the hardest part of this assignment due to my zero knowledge and background of the subject. I had only spent two tutorials (6 hours) learning about this subject and even then I could only manage the basics such as how to set up a wire circuit and alter some coding which together could make function a very small LED light bulb.

Thankfully the internet has an endless supply of knowledge on every topic ever and provided me with some highly useful sources which made my life a lot easier!

My top 5 web pages when learning how to code:

1. Arduino For Dummies

This article clearly explains the process in how motion sensors and Arduino’s are able to be used in conjunction and it provides a guide on how a circuit can be formulated. I found this article vital for my foundation knowledge on how to create a functioning movement sensor reactive Arduino but a drawback of this piece is that it is written for the most basic functions and so it doesn’t explain coding to manipulate variants such as speed.

2. Learning about Electronics 

This website successfully teaches how to create an Arduino motion detector circuit through its clear, labelled, diagrams and accompanying tutorial video. It is written by a website whose purpose is electronics education so I deem this source highly reliable.

3.  Motion Sensor Arduino Tutorial

This visual and audible medium allowed for an easy to follow tutorial in creating an Arduino motion sensor circuit and the visuals helped clarify any issues I had.

4. Tutorial: How to control a Stepper Motor with Arduino and Easy Driver

Garage Lab has a publicised a video tutorial on how to make a stepper motor function through an Arduino and this is greatly advantageous in helping create my circuit. As a primary source and its proven success in being able to make a stepper motor function, I consider this source dependable.

5. Sweep tutorial

This website provides a clear explanation and diagrams which teach on how to make a servo (stepper) motor make a sweeping motion. It also provides detailed coding which is greatly appreciated and due to its highly specific nature, I have found it extremely valuable and cannot find any limitations.

Below is the initial code I used for a distance sensor version of single servo motor with a brief explanation of what each line means after the end // :

#include <Servo.h>

int distance = 0;  // how far away from the sensor we are

Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

void setup()   // this is the first thing that runs when the arduino powers on or is reset
{ pinMode(2,INPUT);   // set pin 2 to be an input
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
Serial.begin(9600);  // initialize serial communications at 9600 bps
}

void loop() // this runs repeatedly after ‘setup’ is finished
{
distance = analogRead(A0);  // read value from analog pin 0 – value is approximately 1 inch per unit
Serial.print(“sensor = ” );   // print some text to the serial monitor
Serial.println(distance);     // print the value of ‘distance’ to the serial monitor

if(distance > 20)
{ myservo.write(90);  // set servo to 90deg
}
else
{ myservo.write(0);  // set servo to 0deg
}
delay(100);  // wait 100 miliseconds
}

———————————————————————————————————

Here is the final code I used with the exact variants for distance, speed and range of movement. Once again I have included a brief explanation.

#include <Servo.h> 
 
const int threshold = 120;    // range of sensor to activate at
const int frontAngle = 160;    // stepper angle at which mirrors face front
const int upAngle = 110;       // stepper angle at which mirrors face up
 
 
 
int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;   // value read from the sensor
 
Servo myservo[16];  // create servo object to contxrol a servo 
 
int pos[16];    // variables to store the servo positions 
 
void setup() 
  Serial.begin(9600); 
  for(int i = 0 ; i < 16 ; i++)
  {  myservo[i].attach(i*2 + 22);
     pos[i] = frontAngle;
     myservo[i].write(pos[i]);         // tell servo to go to its set position
  }
 
 
void loop() 
  sensorValue = analogRead(sensorPin); 
  Serial.print(“sensor = ” );                       
  Serial.println(sensorValue);      
 
  
  if(sensorValue < threshold)
  { for(int i = 0; i < 15; i++)         // goes from servo 0 to 15 
    {
      if(random(sensorValue*4) < (threshold – sensorValue) )
      {
        pos[i]=(upAngle);
        myservo[i].write(pos[i]);         // tell servo to go to its set position
      }
      delay(60);                        // waits 60ms for the servo to reach the position 
    }
  } 
  else
  { for(int i = 0 ; i < 16 ; i++)
    {  
      pos[i] = frontAngle;
      myservo[i].write(pos[i]);         // tell servo to go to its set position
    }
  }
  delay(250);
  
} ​

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s