11.07.2015 Views

ARDX-experimenters-g.. - Oomlout

ARDX-experimenters-g.. - Oomlout

ARDX-experimenters-g.. - Oomlout

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Code (no need to type everything in just)Download the Code from ( http://tinyurl.com/dagyrb )(then simply copy the text and paste it into an empty Arduino Sketch)int motorPin = 9; //pin the motor is connected toCIRC-03void setup() //runs oncevoid motorOnThenOffWithSpeed(){{ int onSpeed = 200;// a number betweenpinMode(motorPin, OUTPUT);//0 (stopped) and 255 (full speed)} int onTime = 2500;int offSpeed = 50;// a number betweenvoid loop() // run over and over again //0 (stopped) and 255 (full speed){ int offTime = 1000;motorOnThenOff(); analogWrite(motorPin, onSpeed);//motorOnThenOffWithSpeed(); // turns the motor On//motorAcceleration(); delay(onTime); // waits for onTime milliseconds} analogWrite(motorPin, offSpeed);// turns the motor Off/* delay(offTime); // waits for offTime milliseconds* motorOnThenOff() - turns motor on then off }used forvoid motorAcceleration(){* the blinking LED)int delayTime = 50; //time between each speed step*/for(int i = 0; i < 256; i++){void motorOnThenOff(){//goes through each speed from 0 to 255int onTime = 2500; //on timeanalogWrite(motorPin, i); //sets the new speedint offTime = 1000; //off timedelay(delayTime);// waits for delayTime millisecondsdigitalWrite(motorPin, HIGH);}// turns the motor Onfor(int i = 255; i >= 0; i--){delay(onTime); // waits for onTime milliseconds//goes through each speed from 255 to 0digitalWrite(motorPin, LOW);analogWrite(motorPin, i); //sets the new speed// turns the motor Offdelay(delayTime);//waits for delayTime millisecondsdelay(offTime);// waits for offTime milliseconds }}}* (notice this code is identical to the code weNot Working? (3 things to try)Motor Not Spinning?if you sourced your owntransistor, double check withthe data sheet that the pinoutis compatible with a P2N2222A(many are reversed)Still No Luck?if you sourced your own motor,double check that it will workwith 5 volts and that it does notdraw too much power.Still Not Working?Sometimes the Arduino boardwill disconnect from thecomputer. Try un-plugging andthen re-plugging it into yourUSB port.Making it BetterControlling Speed:We played with the Arduino's ability to control thebrightness of an LED earlier now we will use the samefeature to control the speed of our motor. The arduinodoes this using something called Pulse WidthModulation (PWM). This relies on the Arduino's ability tooperate really really fast. Rather than directly controllingthe voltage coming from the pin the Arduino will switchthe pin on and off very quickly. In the computer worldthis is going from 0 to 5 volts many times a second, butin the human world we see it as a voltage. For exampleif the Arduino is PWM'ing at 50% we see the lightdimmed 50% because our eyes are not quick enough tosee it flashing on and off. The same feature works withtransistors. Don't believe me? Try it out.In the loop() section change it to this// motorOnThenOff();motorOnThenOffWithSpeed();//motorAcceleration();Then upload the programme. You can change the speeds bychanging the variables onSpeed and offSpeedAccelerating and decelerating:Why stop at two speeds, why not accelerate and deceleratethe motor. To do this simply change the loop() code to read// motorOnThenOff();// motorOnThenOffWithSpeed();motorAcceleration();Then upload the program and watch as your motor slowlyaccelerates up to full speed then slows down again. If youwould like to change the speed of acceleration change thevariable delayTime (larger means a longer acceleration time)More, More, More:More details, where to buy more parts, where to ask more questions.http://tinyurl.com/d4wht713

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!