Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <SoftwareSerial.h>
- const int SENSORS = 128; // TOTAL number of sensors
- const int modules = SENSORS/8; // no of sensor modules
- const int mid = 64 ; // Sensor # parallel to stamp figures
- const int preset = 165; //# of cm actuall value around 100
- const int stampOntime = 25; // How long stamp remain pressed acuall around 25
- // pins
- const int led = 12; // IR led strip
- const int latch = 11;
- const int cloc = 10;
- const int daata = 5;
- const int stampPin = 17; // stamp pin
- const byte proximityPin = 2; // proximity sensor pulses
- const byte checkPin = 3; // from main controler to call check function
- const byte statusPin = 8; // status of connectivity of sensor bar
- volatile byte state = 3; // do nothing state
- volatile byte state2 = 0;
- int counter= 0, counter2 = 0, midSensor = 0 ,len=0 , wid = 0;
- unsigned int sum = 0;
- byte add = 0, stamp = 0 ;
- byte bufferr[modules+1]; //to store data for checking . check if it works with local variable
- //functions
- void sample();
- void chk();
- int readData();
- int checkSensors();
- //SoftwareSerial port(7,9 ); //rx tx
- // the setup function runs once when you press reset or power the board
- void setup() {
- // initialize digital pin LED_BUILTIN as an output.
- pinMode(stampPin, OUTPUT);
- pinMode(led, OUTPUT);
- pinMode(latch, OUTPUT);
- pinMode(cloc,OUTPUT);
- pinMode(daata,INPUT);
- pinMode(proximityPin,INPUT);
- pinMode(checkPin,INPUT);
- pinMode(statusPin,INPUT);
- // pinMode(interruptPin, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(proximityPin), sample, RISING);
- attachInterrupt(digitalPinToInterrupt(checkPin), chk, RISING);
- Serial.begin(57600);
- //port.begin(9600);
- digitalWrite(latch, HIGH);
- digitalWrite(cloc, LOW);
- digitalWrite(led, LOW);
- }
- void loop()
- {
- int c=0;
- byte req ;
- if (state == 1)
- {
- c = readData();
- state = LOW;
- if (c>1)
- {
- if(c>wid)
- // wid=c;
- add=1;
- // len++;
- c=c<<1;
- sum+=c;
- }
- else if(add == 1)
- {
- Serial.print(sum);
- // Serial.println(wid);
- // Serial.println(len);
- sum = 0;
- add = 0;
- len = 0;
- wid = 0;
- }
- if(state2 == 0)
- {
- if(c>0)
- {
- if(midSensor == 1)
- state2 = 2;
- else
- state2 = 1;
- }
- }
- else if(state2 == 1)
- {
- if(midSensor == 1)
- state2 = 2;
- }
- else if(state2 == 2)
- {
- if(midSensor == 0)
- state2 = 3;
- }
- else if(state2 == 3)
- {
- if(c == 0)
- state2 = 4;
- else if(midSensor == 1)
- {
- state2 = 2;
- counter = 0;
- }
- }
- if(counter >= preset)
- {
- digitalWrite(stampPin,HIGH);
- digitalWrite(13,HIGH);
- stamp = 1;
- state2 = 0;
- counter = 0;
- }
- if(stamp == 1)
- counter2++;
- if(counter2 >=stampOntime)
- {
- digitalWrite(stampPin,LOW);
- digitalWrite(13,LOW);
- stamp = 0;
- counter2 = 0;
- }
- //port.print(F(" c = "));
- //port.print(c);
- //port.print(F(" midSenor= "));
- //port.print(midSensor);
- //port.print(F(" state2= "));
- //port.print(state2);
- //port.print(F(" stamp "));
- //port.print(stamp);
- //port.print(F(" counter= "));
- //port.print(counter);
- //port.print(F(" counter2= "));
- //port.println(counter2);
- }
- else if( state == 2)
- {
- checkSensors();
- state = 3;
- }
- else if(Serial.available()>0)
- {
- req=Serial.read();
- if(req == 4)
- state = 0; // ready for measurement
- else
- state = 3; // go into halt position
- }
- //Serial.println(state);
- }
- int readData(){
- int n=0,d=0;
- int i=0;
- noInterrupts();
- //Serial.print("in readData");
- digitalWrite(led, HIGH);
- //delay(1);
- delayMicroseconds(1000);
- digitalWrite(latch,LOW);
- delayMicroseconds(10);
- digitalWrite(led,LOW);
- // just for checking purpose
- // digitalWrite(led, HIGH);
- // delayMicroseconds(10);
- // digitalWrite(latch,HIGH);
- // delayMicroseconds(1000);
- // digitalWrite(latch,LOW);
- // digitalWrite(led,LOW);
- //
- for(i=SENSORS-1;i>=0;i--)
- {
- n= digitalRead(daata);
- if(n==0) // check either c 0 or c 1 if(c!=0)
- d++;
- if (i == mid)
- midSensor = !n;
- //Serial.println(i);
- digitalWrite(cloc, HIGH);
- delayMicroseconds(30);//30
- digitalWrite(cloc, LOW);
- delayMicroseconds(25);//20
- }
- digitalWrite(latch,HIGH);
- //Serial.println(d);
- interrupts();
- return d;
- }
- int checkSensors(){
- // noInterrupts();
- int n1=0,d1=0;
- int i1=0,j1=0 , st=0;
- // byte bufferr[modules+1]; //to store data for checking . check if it works with local variable
- // port.println("In check sensor");
- st=readData();
- if(st == 0)
- bufferr[0] = 0; // to indicate sensors OK
- else
- bufferr[0] = 1; // To indicate some sensors not OK
- st = digitalRead(statusPin);
- if(st == 1)
- bufferr[0] = 2; // to indicate sensor bar not connected
- delay(20);
- digitalWrite(led,HIGH);
- // digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
- delayMicroseconds(1000);
- digitalWrite(latch,LOW); // Ready for serial data out
- delayMicroseconds(10);
- digitalWrite(led,LOW); // turn the LED off (HIGH is the voltage level)
- // digitalWrite(13, LOW); //
- for(i1=modules; i1>0; i1--)
- {
- for(j1=7; j1>=0; j1--)
- {
- n1= digitalRead(daata);
- // port.print(n1);
- if (n1 == 0)
- bufferr[i1] = bufferr[i1] | (1 << j1);
- digitalWrite(cloc, HIGH);
- delayMicroseconds(30); // 30
- digitalWrite(cloc, LOW);
- delayMicroseconds(30); //30
- }
- }
- digitalWrite(latch,HIGH);
- Serial.write(bufferr,modules+1); // check whether this function works
- //Serial.write(bufferr[0]);
- // Serial.write(bufferr[1]);
- // Serial.write(bufferr[2]);
- /*
- port.print(" B0 ");
- port.print(bufferr[0]);
- port.print(" B1 ");
- port.print(bufferr[16]);
- port.print(" B2 ");
- port.println(bufferr[15]);
- */
- // port.print(" ");
- Serial.flush();
- // port.println(d1);3
- for( i1=0;i1<=modules;i1++)
- bufferr[i1]=0; // reset buffer for next value;
- // interrupts();
- return d1;
- }
- void sample(){
- if(state2 == 3 || state2 == 4)
- counter++;
- if(state == 0)
- state = 1;
- // digitalWrite(13,HIGH);
- // Serial.print("int");
- }
- void chk(){
- state=2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement