Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int s[6]; //this is for storing analog value of each sensor
- int thresould[6] = {512,512,512,512,512,512}; //this is the mid value
- int base[6]={1,2,4,8,16,32}; //this is for binary to decimal conversion
- int sensor; //this is to store final value after binary conversion
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- reading();
- 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
- }
- void reading() {
- sensor = 0; //this is to refresh initial value
- for (int i = 0 ; i < 6 ; i++) {
- s[i] = analogRead(i);
- (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.
- 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
- }
- }
- void show_the_value() {
- for (int i = 5 ; i >= 0 ; i++)
- Serial.print(String(s[i], 10) + " ");
- Serial.print(sensor);
- Serial.println();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement