15. November 2010
// 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); }