Guest User

writing.ino

a guest
Feb 13th, 2017
224
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //constants
  2. static const int IN = LOW;
  3. static const int OUT = HIGH;
  4. static const int pulseDelayTime = 6;
  5.  
  6. //pins
  7. int indexPin = 2; //8 on the drive INDEX
  8. int track0Pin = 3; //26 on the drive. TRACK 0
  9. int dirPin = 6; //18 on the drive. DIRECTION
  10. int stepPin = 7; //20 on the drive. STEP
  11. int motorEnableBPin = 9; //16 on the drive. MOTOR ENABLE B
  12. int dataPin = 10; //30 on the drive. RDATA
  13. int writeDataPin = 4; //22 on the drive WDATE
  14. int writeEnablePin = 5; //24 on the drive WGATE
  15.  
  16. byte data[10] = {0};
  17. unsigned int index = 0;
  18. unsigned int pos = 0;
  19.  
  20. void setup() {
  21.   //initial delay
  22.   delay(3000);
  23.  
  24.   //setup serial monitor
  25.   Serial.begin(9600);      
  26.  
  27.   //setup pins.
  28.   pinMode(dirPin, OUTPUT);
  29.   pinMode(stepPin, OUTPUT);
  30.   pinMode(motorEnableBPin, OUTPUT);
  31.   pinMode(indexPin, INPUT_PULLUP);
  32.   pinMode(track0Pin, INPUT_PULLUP);
  33.   pinMode(dataPin, INPUT_PULLUP);
  34.   pinMode(writeDataPin, OUTPUT);
  35.   pinMode(writeEnablePin, OUTPUT);
  36.  
  37.  
  38.   //turn the motor off initially
  39.   digitalWrite(motorEnableBPin, HIGH);
  40.  
  41.   stepAllTheWayOut();
  42.   //print state here.
  43.   Serial.println("Setup done.");
  44.  
  45.   writeData(170);
  46.   delay(500);
  47.   readData();
  48.  
  49. }
  50.  
  51. //byte mode(data[10]){
  52. //  return data[0];
  53. //}
  54.  
  55. //move the head towards the outside a little
  56. void stepOut() {
  57.   digitalWrite(dirPin,HIGH);
  58. //  Serial.println("SteppingOut");
  59.   stepPulse();
  60. }
  61.  
  62. //move the head towards the center a little
  63. void stepIn() {
  64.   digitalWrite(dirPin,LOW);
  65. //  Serial.println("SteppingIn");
  66.   stepPulse();
  67. }
  68.  
  69. //step the read/write head all the way to the center
  70. void stepAllTheWayIn() {
  71.   for(int i=0;i<81;i++) {
  72.     stepIn();
  73.   }
  74. }
  75.  
  76. //step the read/write head all the way to the outside
  77. void stepAllTheWayOut() {
  78.   while (digitalRead(track0Pin) == 1){
  79.     stepOut();
  80.   }
  81. }
  82.  
  83. void writeData(byte data){
  84.   Serial.println("Writing");
  85.   digitalWrite(writeEnablePin, LOW);
  86.   digitalWrite(motorEnableBPin, LOW);
  87.   stepAllTheWayOut();
  88.   for(unsigned i = 0; i<10; i++){
  89.     for(unsigned index = 0; index<=8; index++){
  90.       digitalWrite(writeDataPin,bitRead(data,index));
  91.       Serial.print(bitRead(data,index));
  92.       delay(500);                        
  93.       digitalWrite(writeEnablePin, HIGH);
  94.       stepIn();
  95.       digitalWrite(writeEnablePin, LOW);
  96.     }
  97.     Serial.println("");
  98.   }
  99.   digitalWrite(motorEnableBPin, HIGH);
  100.   digitalWrite(writeEnablePin, HIGH);
  101. }
  102.  
  103. void readData(){
  104.   Serial.println("Reading");
  105.   byte data[10] = {0};
  106.   digitalWrite(motorEnableBPin, LOW);
  107.   digitalWrite(writeEnablePin, HIGH);
  108.   stepAllTheWayOut();
  109.   for(unsigned i = 0; i<10; i++){
  110.     for(unsigned index = 0; index<=8; index++){
  111.       delay(100);
  112.       Serial.print(digitalRead(dataPin));
  113.       stepIn();
  114.     }
  115.     Serial.println("");
  116.   }
  117.   digitalWrite(motorEnableBPin, HIGH);
  118. }
  119.  
  120. void stepPulse() {
  121.   digitalWrite(stepPin,LOW);
  122.   delay(pulseDelayTime);
  123.   digitalWrite(stepPin,HIGH);
  124. }
  125. void loop() {
  126. }
RAW Paste Data