12. November 2010
This was a group project for us to get used to servo, dc and step motors. Our project ended kind of trashy and experimental but in the end the whole process was really fun to us. Lots of gaffa tape have been used. Code für Dispersion der Murmeln// Constants ------------------------------------ #define DIR 2 // Direction Pin #define STEP 3 // Constant #define LED 4 // LED, lights up each motor rotation // Variables ------------------------------------ int latchPin = 8; // latch pin for the led row int dataPin = 11; // data pin int clockPin = 12; // clock pin int steps = 0; // number of steps read over serial communication int sensorValue = 0; // value of the LED sensor // Setup ---------------------------------------- void setup() { pinMode(DIR, OUTPUT); // Direction is an output pinMode(STEP, OUTPUT); // Step is an output pinMode(LED, OUTPUT); pinMode(latchPin, OUTPUT); digitalWrite(LED, LOW); // internal pin up Serial.begin(9600); // set led row off shiftOut(dataPin, clockPin, 0); shiftOut(dataPin, clockPin, 0); } // Loop ----------------------------------------- void loop() { digitalWrite(DIR, LOW); // set direction if (Serial.available() >= 1) { // if serial communication is available steps = Serial.read() -48; // read the number of steps and convert the char into the equivalent number Serial.println(steps); // motorrrr for (int j=0; j<steps; j++) { Serial.println(j); digitalWrite(LED, HIGH); // turn single led on stepOnce(); // do 1 step delay(850); // time one marble takes from the top to the led row digitalWrite(LED, LOW); light(); // ledsssss } } } // Function Definition --------------------------- void stepOnce() { // motor rotation for (int i=0; i<=24; i++) { digitalWrite(STEP, HIGH); delay(10); digitalWrite(STEP, LOW); delay(1); } } void light () { // led row for (int j = 0; j < 8; j++) { //ground latchPin and hold low for as long as you are transmitting digitalWrite(latchPin, 0); shiftOut(dataPin, clockPin, 0); lightShiftPinA(j); digitalWrite(latchPin, 1); delay(50); } for (int j = 0; j < 8; j++) { //ground latchPin and hold low for as long as you are transmitting lightShiftPinA(j); digitalWrite(latchPin, 0); shiftOut(dataPin, clockPin, 0); digitalWrite(latchPin, 1); delay(50); } } void lightShiftPinA(int p) { int pin; pin = 1<< p; shiftOut(dataPin, clockPin, pin); } // the heart of the program void shiftOut(int myDataPin, int myClockPin, byte myDataOut) { // This shifts 8 bits out MSB first, //on the rising edge of the clock, //clock idles low //internal function setup int i=0; int pinState; pinMode(myClockPin, OUTPUT); pinMode(myDataPin, OUTPUT); //clear everything out just in case to //prepare shift register for bit shifting digitalWrite(myDataPin, 0); digitalWrite(myClockPin, 0); //for each bit in the byte myDataOut� //NOTICE THAT WE ARE COUNTING DOWN in our for loop //This means that %00000001 or "1" will go through such //that it will be pin Q0 that lights. for (i=7; i>=0; i--) { digitalWrite(myClockPin, 0); //if the value passed to myDataOut and a bitmask result // true then... so if we are at i=6 and our value is // %11010100 it would the code compares it to %01000000 // and proceeds to set pinState to 1. if ( myDataOut & (1<<i) ) { pinState= 1; } else { pinState= 0; } //Sets the pin to HIGH or LOW depending on pinState digitalWrite(myDataPin, pinState); //register shifts bits on upstroke of clock pin digitalWrite(myClockPin, 1); //zero the data pin after shift to prevent bleed through digitalWrite(myDataPin, 0); } //stop shifting digitalWrite(myClockPin, 0); }Code für Bewegung des Xylophons
#define DIR 10 #define STEP 11 int stepWidth = 60; void setup() { pinMode(DIR, OUTPUT); pinMode(STEP, OUTPUT); Serial.begin(9600); } void loop() { goOne(); goTwo(); goThree(); goFour(); goFive(); goSix(); } void goOne() { Serial.println("One"); digitalWrite(DIR, HIGH); for (int i = 0; i <= stepWidth; i++) { doStep(); } delay(500); } void goTwo() { Serial.println("Two"); digitalWrite(DIR, HIGH); for (int i = 0; i <= stepWidth; i++) { doStep(); } delay(500); } void goThree() { Serial.println("Three"); digitalWrite(DIR, HIGH); for (int i = 0; i <= stepWidth; i++) { doStep(); } delay(500); } void goFour() { Serial.println("Four"); digitalWrite(DIR, LOW); for (int i = 0; i <= stepWidth; i++) { doStep(); } delay(500); } void goFive() { Serial.println("Five"); digitalWrite(DIR, LOW); for (int i = 0; i <= stepWidth; i++) { doStep(); } delay(500); } void goSix() { Serial.println("Six"); digitalWrite(DIR, LOW); for (int i = 0; i <= stepWidth; i++) { doStep(); } delay(500); } void doStep(){ digitalWrite(STEP, HIGH); delay(5); digitalWrite(STEP, LOW); delay(5); }