SHARE
TWEET

Untitled

a guest Jan 12th, 2017 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <Servo.h>
  3.  
  4. //Pin connected to ST_CP of 74HC595
  5. int latchPin = 8;
  6. //Pin connected to SH_CP of 74HC595
  7. int clockPin = 12;
  8. ////Pin connected to DS of 74HC595
  9. int dataPin = 11;
  10.  
  11. int dlay = 20;
  12. //holders for infromation you're going to pass to shifting function
  13. byte data;
  14. byte dataArray[10];
  15.  
  16. Servo myservo;
  17.  
  18. int val;  
  19.  
  20. void setup() {
  21.   //set pins to output because they are addressed in the main loop
  22.   pinMode(latchPin, OUTPUT);
  23.   Serial.begin(9600);
  24.   myservo.attach(9);
  25.  
  26. }
  27.  
  28.   int rotNum = 0;
  29.   int encoder0PinALast = LOW;
  30.   int n = LOW;
  31.  
  32. void loop() {
  33.  
  34.  
  35.   n = digitalRead(4);
  36.   if ((encoder0PinALast == LOW) && (n == HIGH)) {
  37.     if (digitalRead(7) == LOW) {
  38.       rotNum--;
  39.       delay(dlay);
  40.     } else {
  41.       rotNum++;
  42.       delay(dlay);
  43.     }
  44.   }
  45.   encoder0PinALast = n;
  46.   if (rotNum > 99) {
  47.     rotNum = 0;
  48.   }
  49.   if (rotNum < 0) {
  50.     rotNum = 99;
  51.   }
  52.   shiftNumber(rotNum);
  53.   val = rotNum;            // reads the value of the potentiometer (value between 0 and 1023)
  54.   val = map(val, 0, 99, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  55.   myservo.write(val);                  // sets the servo position according to the scaled value
  56. }
  57.  
  58.  
  59. void shiftNumber(int myInput) {
  60.   //ground latchPin and hold low for as long as you are transmitting
  61.   digitalWrite(latchPin, 0);
  62.   //move 'em out
  63.  
  64.   int b = myInput % 10;
  65.   int a = (myInput - b) / 10;
  66.   int temp = a * 16 + b;
  67.   shiftOut(dataPin, clockPin, temp);
  68.   //return the latch pin high to signal chip that it
  69.   //no longer needs to listen for information
  70.   digitalWrite(latchPin, 1);
  71. }
  72.  
  73.  
  74.  
  75.  
  76. // the heart of the program
  77. void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  78.   // This shifts 8 bits out MSB first,
  79.   //on the rising edge of the clock,
  80.   //clock idles low
  81.  
  82.   //internal function setup
  83.   int i = 0;
  84.   int pinState;
  85.   pinMode(myClockPin, OUTPUT);
  86.   pinMode(myDataPin, OUTPUT);
  87.  
  88.   //clear everything out just in case to
  89.   //prepare shift register for bit shifting
  90.   digitalWrite(myDataPin, 0);
  91.   digitalWrite(myClockPin, 0);
  92.  
  93.   //for each bit in the byte myDataOut´┐Ż
  94.   //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  95.   //This means that %00000001 or "1" will go through such
  96.   //that it will be pin Q0 that lights.
  97.   for (i = 7; i >= 0; i--)  {
  98.     digitalWrite(myClockPin, 0);
  99.  
  100.     //if the value passed to myDataOut and a bitmask result
  101.     // true then... so if we are at i=6 and our value is
  102.     // %11010100 it would the code compares it to %01000000
  103.     // and proceeds to set pinState to 1.
  104.     if ( myDataOut & (1 << i) ) {
  105.       pinState = 1;
  106.     }
  107.     else {
  108.       pinState = 0;
  109.     }
  110.  
  111.     //Sets the pin to HIGH or LOW depending on pinState
  112.     digitalWrite(myDataPin, pinState);
  113.     //register shifts bits on upstroke of clock pin
  114.     digitalWrite(myClockPin, 1);
  115.     //zero the data pin after shift to prevent bleed through
  116.     digitalWrite(myDataPin, 0);
  117.   }
  118.  
  119.   //stop shifting
  120.   digitalWrite(myClockPin, 0);
  121. }
RAW Paste Data
Top