Traffic Light/Servomotor Assignment (Jag)

Done and dusted! Today the traffic light assignment was completed within the first hour of our project. I decided to simplify the model by reducing the amount of wires we were using.  This was done by connecting straight to the center of the bread board instead of using 2 wires for each color, connecting one to the periphery and another from the periphery to the center.  I realized that in the previous attempts, I had forgotten how the cross section of the bread board looked like. I attached the black wire to the positive line instead.

As for the code itself, I learnt that I could organize the code by time: for the first 5 seconds, I want xxx on and xxx off, for the next 5 seconds, I want ... so on and so forth.


Assignment #2: Servomotors

Brown - grd
Red - VCC - 5V (connect directly to microcontroller)
orange

We removed the traffic light instalment and focused on initiating the movement of a servomotor first.
Here is the code and the results.



Code
And the results:

And combining them together:



And the results:



Here is the code:
[code]
#include<Servo.h>
Servo servoj;
int pos1 = 0;
void setup() {
  // put your setup code here, to run once:
pinMode(2,OUTPUT); //red
pinMode(4,OUTPUT); //yellow
pinMode(6,OUTPUT); //green
servoj.attach(9);
servoj.write(180);
}
void loop()
{
  // red on for 5 sec and yellow + green off for 5 sec
 for(pos1 = 0; pos1 < 180; pos1 += 1)
 digitalWrite(2,1); //red on
digitalWrite(4,0);
digitalWrite(6,0);
servoj.write(pos1);
delay(1000);
// red + green off for 5 sec and yellow on for 5
digitalWrite(2,0);
digitalWrite(4,1);
digitalWrite(6,0);
delay(1000);
// green on, red and yellow off for 5
for(pos1 = 180;pos1>=1; pos1-=1)
digitalWrite(2,0);
digitalWrite(4,0);
digitalWrite(6,1); //green on
servoj.write(pos1);

delay(1000);
}
[/code]



Comments

Popular Posts