Hi Ross.
One more step. I have experimented with Arduino re outputting binary on the digital I?O pins. It turned out far simpler than I anticipated.
It seems there is no need to type HIGH or LOW to switch pins on or off. “1” or “0” works just as well.
The “bitRead” command returns a “1” or “0”.
I tested the following sketch using a potentiometer as I don’t have your sensor and it works well. Herewith sketch:
//Sketch to output Binary value of a number to Digital I/O pins Arduino:
//Bob Rayner 13/04/21:
//Assign pin names:
const int Bit0 = 2;
const int Bit1 = 3;
const int Bit2 = 4;
const int Bit3 = 5;
const int Bit4 = 6;
const int Bit5 = 7;
int potpin = 0; //analog pin used to connect potentiometer
int val; //variable to read the value from the analog pin
void setup() {
// put your setup code here, to run once:
//Declare assigned pins as outputs:
pinMode(Bit0, OUTPUT);
pinMode(Bit1, OUTPUT);
pinMode(Bit2, OUTPUT);
pinMode(Bit3, OUTPUT);
pinMode(Bit4, OUTPUT);
pinMode(Bit5, OUTPUT);
Serial.begin( 9600 );
}
void loop() {
// put your main code here, to run repeatedly:
val = analogRead(potpin); // reads value of pot (between 0 and 1023):
val = map(val,0,1023,0,39);// scale it to use with resistor ladder:
//Note map is 0-39. There are 38 steps required and mapping did not toggle:
//to 38 until 1023. mapping to 39 results in 38 distinct areas:
//Samples input number (val) and writes binary value to relevant bit output pin. High active (1):
digitalWrite(Bit5, bitRead(val, 5));
digitalWrite(Bit4, bitRead(val, 4));
digitalWrite(Bit3, bitRead(val, 3));
digitalWrite(Bit2, bitRead(val, 2));
digitalWrite(Bit1, bitRead(val, 1));
digitalWrite(Bit0, bitRead(val, 0));
delay(1000);// Delay to sample every second. Change to suit requirement:
//Print info to serial monitor for diagnostic or check purposes. Comment out when not required:
Serial.print(analogRead(potpin));
Serial.print(", “);
Serial.print(val);
Serial.print(”, ");
Serial.print(bitRead(val,5));
Serial.print(bitRead(val,4));
Serial.print(bitRead(val,3));
Serial.print(bitRead(val,2));
Serial.print(bitRead(val,1));
Serial.println(bitRead(val,0));
}
Sorry it would not upload as a sketch. There must be a way of doing it as others have done it OK. Copied via clipboard as text.
So there you just about have it. What you have to do now is interface your sensor with Arduino, re map ADC output to 0 - 39. Use this sketch and the output interface circuit I produced previously and you should be in business.
Note. I had to re map to “39” as 38 did not toggle until ADC reached 1023. Doing this provides 38 clearly defined sections of the range.
Cheers Bob