Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. //#include <SoftwareSerial.h>
  2.  
  3. const int SENSORS = 128; // TOTAL number of sensors
  4. const int modules = SENSORS/8; // no of sensor modules
  5. const int mid = 64 ; // Sensor # parallel to stamp figures
  6. const int preset = 165; //# of cm actuall value around 100
  7. const int stampOntime = 25; // How long stamp remain pressed acuall around 25
  8.  
  9. // pins
  10. const int led = 12; // IR led strip
  11. const int latch = 11;
  12. const int cloc = 10;
  13. const int daata = 5;
  14. const int stampPin = 17; // stamp pin
  15. const byte proximityPin = 2; // proximity sensor pulses
  16. const byte checkPin = 3; // from main controler to call check function
  17. const byte statusPin = 8; // status of connectivity of sensor bar
  18.  
  19. volatile byte state = 3; // do nothing state
  20. volatile byte state2 = 0;
  21.  
  22. int counter= 0, counter2 = 0, midSensor = 0 ,len=0 , wid = 0;
  23. unsigned int sum = 0;
  24. byte add = 0, stamp = 0 ;
  25. byte bufferr[modules+1]; //to store data for checking . check if it works with local variable
  26.  
  27.  
  28. //functions
  29.  
  30. void sample();
  31. void chk();
  32. int readData();
  33. int checkSensors();
  34.  
  35. //SoftwareSerial port(7,9 ); //rx tx
  36.  
  37. // the setup function runs once when you press reset or power the board
  38. void setup() {
  39. // initialize digital pin LED_BUILTIN as an output.
  40. pinMode(stampPin, OUTPUT);
  41. pinMode(led, OUTPUT);
  42. pinMode(latch, OUTPUT);
  43. pinMode(cloc,OUTPUT);
  44. pinMode(daata,INPUT);
  45. pinMode(proximityPin,INPUT);
  46. pinMode(checkPin,INPUT);
  47. pinMode(statusPin,INPUT);
  48.  
  49.  
  50. // pinMode(interruptPin, INPUT_PULLUP);
  51. attachInterrupt(digitalPinToInterrupt(proximityPin), sample, RISING);
  52. attachInterrupt(digitalPinToInterrupt(checkPin), chk, RISING);
  53.  
  54. Serial.begin(57600);
  55. //port.begin(9600);
  56. digitalWrite(latch, HIGH);
  57. digitalWrite(cloc, LOW);
  58. digitalWrite(led, LOW);
  59. }
  60.  
  61. void loop()
  62. {
  63. int c=0;
  64. byte req ;
  65. if (state == 1)
  66. {
  67. c = readData();
  68. state = LOW;
  69. if (c>1)
  70. {
  71. if(c>wid)
  72. // wid=c;
  73. add=1;
  74. // len++;
  75. c=c<<1;
  76. sum+=c;
  77. }
  78. else if(add == 1)
  79. {
  80. Serial.print(sum);
  81. // Serial.println(wid);
  82. // Serial.println(len);
  83.  
  84. sum = 0;
  85. add = 0;
  86. len = 0;
  87. wid = 0;
  88. }
  89. if(state2 == 0)
  90. {
  91. if(c>0)
  92. {
  93. if(midSensor == 1)
  94. state2 = 2;
  95. else
  96. state2 = 1;
  97. }
  98. }
  99. else if(state2 == 1)
  100. {
  101. if(midSensor == 1)
  102.  
  103. state2 = 2;
  104. }
  105. else if(state2 == 2)
  106. {
  107.  
  108. if(midSensor == 0)
  109. state2 = 3;
  110.  
  111. }
  112. else if(state2 == 3)
  113. {
  114. if(c == 0)
  115. state2 = 4;
  116. else if(midSensor == 1)
  117. {
  118. state2 = 2;
  119. counter = 0;
  120. }
  121. }
  122. if(counter >= preset)
  123. {
  124.  
  125. digitalWrite(stampPin,HIGH);
  126. digitalWrite(13,HIGH);
  127. stamp = 1;
  128. state2 = 0;
  129. counter = 0;
  130. }
  131. if(stamp == 1)
  132. counter2++;
  133. if(counter2 >=stampOntime)
  134. {
  135. digitalWrite(stampPin,LOW);
  136. digitalWrite(13,LOW);
  137. stamp = 0;
  138. counter2 = 0;
  139. }
  140.  
  141. //port.print(F(" c = "));
  142. //port.print(c);
  143. //port.print(F(" midSenor= "));
  144. //port.print(midSensor);
  145. //port.print(F(" state2= "));
  146. //port.print(state2);
  147. //port.print(F(" stamp "));
  148. //port.print(stamp);
  149. //port.print(F(" counter= "));
  150. //port.print(counter);
  151. //port.print(F(" counter2= "));
  152. //port.println(counter2);
  153. }
  154. else if( state == 2)
  155. {
  156. checkSensors();
  157. state = 3;
  158. }
  159. else if(Serial.available()>0)
  160. {
  161. req=Serial.read();
  162. if(req == 4)
  163. state = 0; // ready for measurement
  164. else
  165. state = 3; // go into halt position
  166. }
  167.  
  168. //Serial.println(state);
  169. }
  170.  
  171.  
  172. int readData(){
  173. int n=0,d=0;
  174. int i=0;
  175. noInterrupts();
  176. //Serial.print("in readData");
  177. digitalWrite(led, HIGH);
  178. //delay(1);
  179. delayMicroseconds(1000);
  180. digitalWrite(latch,LOW);
  181. delayMicroseconds(10);
  182. digitalWrite(led,LOW);
  183.  
  184. // just for checking purpose
  185.  
  186. // digitalWrite(led, HIGH);
  187. // delayMicroseconds(10);
  188. // digitalWrite(latch,HIGH);
  189. // delayMicroseconds(1000);
  190. // digitalWrite(latch,LOW);
  191. // digitalWrite(led,LOW);
  192. //
  193. for(i=SENSORS-1;i>=0;i--)
  194. {
  195. n= digitalRead(daata);
  196. if(n==0) // check either c 0 or c 1 if(c!=0)
  197. d++;
  198. if (i == mid)
  199. midSensor = !n;
  200. //Serial.println(i);
  201.  
  202.  
  203. digitalWrite(cloc, HIGH);
  204. delayMicroseconds(30);//30
  205. digitalWrite(cloc, LOW);
  206. delayMicroseconds(25);//20
  207. }
  208. digitalWrite(latch,HIGH);
  209. //Serial.println(d);
  210. interrupts();
  211. return d;
  212. }
  213.  
  214. int checkSensors(){
  215.  
  216. // noInterrupts();
  217. int n1=0,d1=0;
  218. int i1=0,j1=0 , st=0;
  219. // byte bufferr[modules+1]; //to store data for checking . check if it works with local variable
  220.  
  221.  
  222. // port.println("In check sensor");
  223. st=readData();
  224. if(st == 0)
  225. bufferr[0] = 0; // to indicate sensors OK
  226. else
  227. bufferr[0] = 1; // To indicate some sensors not OK
  228.  
  229. st = digitalRead(statusPin);
  230. if(st == 1)
  231. bufferr[0] = 2; // to indicate sensor bar not connected
  232. delay(20);
  233.  
  234. digitalWrite(led,HIGH);
  235. // digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
  236. delayMicroseconds(1000);
  237. digitalWrite(latch,LOW); // Ready for serial data out
  238. delayMicroseconds(10);
  239. digitalWrite(led,LOW); // turn the LED off (HIGH is the voltage level)
  240. // digitalWrite(13, LOW); //
  241. for(i1=modules; i1>0; i1--)
  242. {
  243. for(j1=7; j1>=0; j1--)
  244. {
  245. n1= digitalRead(daata);
  246. // port.print(n1);
  247. if (n1 == 0)
  248. bufferr[i1] = bufferr[i1] | (1 << j1);
  249.  
  250.  
  251. digitalWrite(cloc, HIGH);
  252. delayMicroseconds(30); // 30
  253. digitalWrite(cloc, LOW);
  254. delayMicroseconds(30); //30
  255. }
  256. }
  257. digitalWrite(latch,HIGH);
  258.  
  259. Serial.write(bufferr,modules+1); // check whether this function works
  260. //Serial.write(bufferr[0]);
  261. // Serial.write(bufferr[1]);
  262. // Serial.write(bufferr[2]);
  263.  
  264. /*
  265. port.print(" B0 ");
  266. port.print(bufferr[0]);
  267. port.print(" B1 ");
  268. port.print(bufferr[16]);
  269. port.print(" B2 ");
  270. port.println(bufferr[15]);
  271. */
  272. // port.print(" ");
  273. Serial.flush();
  274. // port.println(d1);3
  275.  
  276. for( i1=0;i1<=modules;i1++)
  277. bufferr[i1]=0; // reset buffer for next value;
  278. // interrupts();
  279. return d1;
  280. }
  281. void sample(){
  282. if(state2 == 3 || state2 == 4)
  283. counter++;
  284. if(state == 0)
  285. state = 1;
  286. // digitalWrite(13,HIGH);
  287. // Serial.print("int");
  288. }
  289. void chk(){
  290. state=2;
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement