Advertisement
Anik_Akash

senor advance

Aug 28th, 2020
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int s[6]; //this is for storing analog value of each sensor
  2. int thresould[6] = {512,512,512,512,512,512}; //this is the mid value
  3. int base[6]={1,2,4,8,16,32}; //this is for binary to decimal conversion
  4. int sensor; //this is to store final value after binary conversion
  5.  
  6. void setup() {
  7.   Serial.begin(9600);
  8. }
  9.  
  10. void loop() {
  11.   reading();
  12.   show_the_value(); //this is to check the sensor reading in serial monitor. comment this out if you make the robot line follow because this code slows the robot
  13.  
  14. }
  15. void reading() {
  16.   sensor = 0; //this is to refresh initial value
  17.   for (int i = 0 ; i < 6 ; i++) {
  18.     s[i] = analogRead(i);
  19.     (s[i] > thresould[i]) ? s[i] = 1 : s[i] = 0; //conditional statement. this is to convert analog value to digital. if you want to see real analog value, then comment it. but for line follow, you must uncomment it.
  20.     sensor += s[i] * base[i]; //this is to merge all 6 values and imagine them in a single binary number. then i converted it into decimal number to use as final value. better search about base convertion
  21.   }
  22. }
  23.  
  24. void show_the_value() {
  25.   for (int i = 5 ; i >= 0 ; i++)
  26.     Serial.print(String(s[i], 10) + " ");
  27.   Serial.print(sensor);
  28.   Serial.println();
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement