Wired results with HC-SR04
i using hc-sr04 ultrasonic sensor. have tested , works fine.
in program transferring data on 16 wires raspberry pi.
if put relevant ultrasonic code in void loop works fine.
if put relevant ultrasonic code in in function , call in void loop half results 0 when transmitted raspberry pi if sensor seeing nothing. paradox same results fine in serial monitor.
i have tested code on raspberry pi side , works fine.
my code wip , things might wired, irrelevant, or out of place, have not coded yet.
working code:
buggy code:
in program transferring data on 16 wires raspberry pi.
if put relevant ultrasonic code in void loop works fine.
if put relevant ultrasonic code in in function , call in void loop half results 0 when transmitted raspberry pi if sensor seeing nothing. paradox same results fine in serial monitor.
i have tested code on raspberry pi side , works fine.
my code wip , things might wired, irrelevant, or out of place, have not coded yet.
working code:
code: [select]
int outputpin = 39;
int aclock = 22;
int rclock = 23;
int type = 0;
int header = 0;
int sign = 0;
int value = 0;
int repeat = 0;
int triggerpin = 52;
int echopin = 53;
int duration = 0;
int distance = 0;
void setup() {
// booring setup part program setup here setup here setup here
serial.begin(9600);
// changepinmode(output);
for(int = 24; < 40; i++) {
pinmode(i, output);
}
for(int = 24; < 40; i++) {
digitalwrite(i, low);
}
for(int = 24; < 40; i++) {
pinmode(i, output);
}
pinmode(44, output);
pinmode(42, output);
pinmode(43, output);
pinmode(triggerpin, output);
pinmode(echopin, input);
}
// fun stuff happens
void loop() {
// loopy thing / part of teh program loop here loop here loop here
// digitalwrite(aclock, low);
// nessary stuff
digitalwrite(triggerpin, low);
delaymicroseconds(5);
digitalwrite(triggerpin, high);
delaymicroseconds(10);
digitalwrite(triggerpin, low);
// duration between pulses
duration = pulsein(echopin, high);
// calculate distance based on duration
distance = (duration / 2) / 29.1;
serial.print(distance);
serial.println("cm");
transmit(distance, 1);
// dorecive();
// serial.println();
type = 0;
header = 0;
sign = 0;
value = 0;
repeat = 0;
delay(100);
}
// transmitting code transmit here transmit here transmit here
void transmit(int val, int sensp) {
for(int = 9, x = 39; (i > -1) && (x > 23); i--, x--) {
digitalwrite(x, bitread(val, i));
}
for(int = 5, x = 29; (i > -1) && (x > 23); i--, x--) {
digitalwrite(x, bitread(sensp, i));
}
}
// recive code recive here recive here recive here
void recive() {
// type
if(digitalread(39) == high) type = type + 2;
if(digitalread(38) == high) type = type + 1;
//header
if(digitalread(37) == high) header = header + 4;
if(digitalread(36) == high) header = header + 2;
if(digitalread(35) == high) header = header + 1;
//sign
if(digitalread(34) == high) sign = sign + 1;
//value
if(digitalread(33) == high) value = value + 256;
if(digitalread(32) == high) value = value + 128;
if(digitalread(31) == high) value = value + 64;
if(digitalread(30) == high) value = value + 32;
if(digitalread(29) == high) value = value + 16;
if(digitalread(28) == high) value = value + 8;
if(digitalread(27) == high) value = value + 4;
if(digitalread(26) == high) value = value + 2;
if(digitalread(25) == high) value = value + 1;
//repeat
if(digitalread(24) == high) repeat = repeat + 1;
}
void dorecive() {
recive();
if(type == 0) {
if(sign == 1) {
digitalwrite(42, high);
digitalwrite(43, low);
}
else {
digitalwrite(42, low);
digitalwrite(43, high);
}
analogwrite(44, value);
}
}
// ultrasonic sensor code here ultrasonic here ultrasonic here ultrasonic here
int ultrasonic() {
// nessary stuff
digitalwrite(triggerpin, low);
delaymicroseconds(5);
digitalwrite(triggerpin, high);
delaymicroseconds(10);
digitalwrite(triggerpin, low);
// duration between pulses
duration = pulsein(echopin, high);
// calculate distance based on duration
distance = (duration / 2) / 29.1;
return distance;
}
//int changepinmode(boolean mode) {
// for(int = 24; < 40; i++) {
// pinmode(i, mode);
// }
//}
buggy code:
code: [select]
int outputpin = 39;
int aclock = 22;
int rclock = 23;
int type = 0;
int header = 0;
int sign = 0;
int value = 0;
int repeat = 0;
int triggerpin = 52;
int echopin = 53;
int duration = 0;
int distance = 0;
void setup() {
// booring setup part program setup here setup here setup here
serial.begin(9600);
// changepinmode(output);
for(int = 24; < 40; i++) {
pinmode(i, output);
}
for(int = 24; < 40; i++) {
digitalwrite(i, low);
}
for(int = 24; < 40; i++) {
pinmode(i, output);
}
pinmode(44, output);
pinmode(42, output);
pinmode(43, output);
pinmode(triggerpin, output);
pinmode(echopin, input);
}
// fun stuff happens
void loop() {
// loopy thing / part of teh program loop here loop here loop here
// digitalwrite(aclock, low);
serial.print(ultrasonic());
serial.println("cm");
transmit(ultrasonic(), 1);
// dorecive();
// serial.println();
type = 0;
header = 0;
sign = 0;
value = 0;
repeat = 0;
delay(100);
}
// transmitting code transmit here transmit here transmit here
void transmit(int val, int sensp) {
for(int = 9, x = 39; (i > -1) && (x > 23); i--, x--) {
digitalwrite(x, bitread(val, i));
}
for(int = 5, x = 29; (i > -1) && (x > 23); i--, x--) {
digitalwrite(x, bitread(sensp, i));
}
}
// recive code recive here recive here recive here
void recive() {
// type
if(digitalread(39) == high) type = type + 2;
if(digitalread(38) == high) type = type + 1;
//header
if(digitalread(37) == high) header = header + 4;
if(digitalread(36) == high) header = header + 2;
if(digitalread(35) == high) header = header + 1;
//sign
if(digitalread(34) == high) sign = sign + 1;
//value
if(digitalread(33) == high) value = value + 256;
if(digitalread(32) == high) value = value + 128;
if(digitalread(31) == high) value = value + 64;
if(digitalread(30) == high) value = value + 32;
if(digitalread(29) == high) value = value + 16;
if(digitalread(28) == high) value = value + 8;
if(digitalread(27) == high) value = value + 4;
if(digitalread(26) == high) value = value + 2;
if(digitalread(25) == high) value = value + 1;
//repeat
if(digitalread(24) == high) repeat = repeat + 1;
}
void dorecive() {
recive();
if(type == 0) {
if(sign == 1) {
digitalwrite(42, high);
digitalwrite(43, low);
}
else {
digitalwrite(42, low);
digitalwrite(43, high);
}
analogwrite(44, value);
}
}
// ultrasonic sensor code here ultrasonic here ultrasonic here ultrasonic here
int ultrasonic() {
// nessary stuff
digitalwrite(triggerpin, low);
delaymicroseconds(5);
digitalwrite(triggerpin, high);
delaymicroseconds(10);
digitalwrite(triggerpin, low);
// duration between pulses
duration = pulsein(echopin, high);
// calculate distance based on duration
distance = (duration / 2) / 29.1;
return distance;
}
//int changepinmode(boolean mode) {
// for(int = 24; < 40; i++) {
// pinmode(i, mode);
// }
//}
so write 0 or 1s lines , don't keep values there short while overwrite them sending sensor number on low side of same lines? weirder write them 1 after other higher bits ready before lower bits , right after overwrite them.... how can guarantee raspberry synchronized read data?
you should have kind of clock signal on separate wire going high instructing raspberry data there read , keep them on little while, clock goes low, write other value , put clock high again little while.
you should have kind of clock signal on separate wire going high instructing raspberry data there read , keep them on little while, clock goes low, write other value , put clock high again little while.
Arduino Forum > Using Arduino > Programming Questions > Wired results with HC-SR04
arduino
Comments
Post a Comment