Im trying to write a binary search algorithm using an arduin

I\'m trying to write a binary search algorithm using an arduino for an ADC circuit. The arduino will guess one of the voltages corresponding to decimal values 0-7. If I have 0-7, a total range n=8, then the minimum # of guesses should be logn(8)=3.

Vguess = (Vupper+Vlower)/2

Vlower < Vin < Vupper

So far I have:

void loop() {

for(i=0;i<5;i++)

{

guess = (upper+lower)/2; output_voltage(guess);

if(read_pin(pinNo)==1) {  

..Rest of the code

}

display_digit(guess);  
lower = 0;

upper = 8;   

}

Solution

I am not familiar with the ADC circuit and pins, but I can see you went wrong with the binary search Algorithm. Here is the recursive routine for Binary Search algorithm in python, hope it helps you. Best of luck implementing it.

def binary_search(sorted_arr, low, upper, element_to_find):


   if low > upper:
       print \"element is not there \"
       return -1
   mid = (low+upper)/2

   if sorted_arr[mid] == element_to_find:
       return mid
   else:
       if sorted_arr[mid] > element_to_find:
           # element is in lower half of the array
           return binary_search(sorted_arr, low, mid-1, element_to_find)
       else:
           return binary_search(sorted_arr, mid+1, upper, element_to_find)

I\'m trying to write a binary search algorithm using an arduino for an ADC circuit. The arduino will guess one of the voltages corresponding to decimal values 0

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site