I would like to see if it is possible to read an analog input pin with a 4051 and read a digital pin with an ultrasound sensor at the same time (or as close as).
here's what i've got so far:
int pingPin=8;
int MuxPin1=2;
int MuxPin2=3;
int MuxPin3=4;
int AnaInPin1=4;
//Declare variables
int val = 0;
int pingValue = 0;
int timecount=0;
int MuxVal1 = 0;
int MuxVal2 = 0;
int MuxVal3 = 0;
int BinVal = 0;
int N = 0;
byte data_send = 0;
byte data_in = 0;
//BinPat is used for figuring out the High / Low (1/0) values of the MUX control pins
int BinPat [] = {000, 1, 10, 11, 100, 101, 110, 111};
void setup()
{
//Initialize digital pins and serial communication speed
pinMode(MuxPin1, OUTPUT);
pinMode(MuxPin2, OUTPUT);
pinMode(MuxPin3, OUTPUT);
beginSerial(19200);
}
void loop() {
if(Serial.available() > 0) {
data_in = Serial.read();
if(data_in == 76) {
readPotValues();
readPingValues();
}
}
delay (10);
}
void readPotValues()
{
for (N=0; N<=7; N++)
{
BinVal = BinPat[N];
MuxVal1 = BinVal & 0x01;
MuxVal2 = (BinVal>>1) & 0x01;
MuxVal3 = (BinVal>>2) & 0x01;
digitalWrite(MuxPin1, MuxVal1);
digitalWrite(MuxPin2, MuxVal2);
digitalWrite(MuxPin3, MuxVal3);
}
}
void readPingValues()
{
timecount = 0;
val = 0;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(pingPin, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(pingPin, LOW); // Holdoff
pinMode(pingPin, INPUT); // Switch signalpin to input
val = digitalRead(pingPin); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(pingPin);
}
while(val == HIGH) {
val = digitalRead(pingPin);
timecount = timecount +1;
}
pingValue = timecount;
data_send=analogRead(AnaInPin1) / 4;
Serial.print(data_send);
Serial.print(pingValue);
Serial.flush
}