Reputation: 1
I am brand new to BBB. I have a some experience with an Arduino UNO and want to change things up a bit by utilizing my BBB.
EDIT (20, July): I should mention that I am programming on Windows 10. I don't know if that'll help or not, just thought that I should mention it.
I made a project on Arduino's IDE that measured the time between to LDRs. Here is the code to that:
const int THRESHOLD_LOW = 550; // low point of LDR reading
const int THRESHOLD_HIGH = 625; // high point of LDR reading
// HystereticRead takes the analog input of the LDRs and converts it to logic
// devices
int HystereticRead(int pin, int previousState) {
int photo = analogRead(pin);
if (previousState == LOW && photo >= THRESHOLD_HIGH) {
return HIGH;
} else if (previousState == HIGH && photo < THRESHOLD_LOW) {
return LOW;
} else {
return previousState;
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
static int state0, state1;
static double time0, time1, time2;
int new_state = HystereticRead(A0, state0);
if (state0 == LOW && new_state == HIGH) {
time0 = millis();
}
state0 = new_state;
new_state = HystereticRead(A1, state1);
if (state1 == LOW && new_state == HIGH) {
time1 = millis();
time2 = (time1-time0)/1000;
Serial.println("Time passed: (s)");
Serial.println(time2);
}
state1 = new_state;
}
I am using cloud9 to program my BBB and tried to imitate my Arduino project. This is what I have:
var b = require('bonescript');
var LDR = 'P9_38';
var LDR1 = 'P9_40';
const THRESHOLD_LOW = 250;
const THRESHOLD_HIGH = 550;
function AnalogToDigital(LDR, previousState) {
var light = analoglogRead(LDR);
if(previousState == b.LOW && light >= THRESHOLD_HIGH) {
return b.HIGH;
} else if(previousState == b.HIGH && light < THRESHOLD_LOW) {
return b.LOW;
} else {
return previousState;
}
}
function readLightLoop() {
var state0, state1;
var time0, time1, time2;
var new_state = AnalogToDigital(LDR, state0);
if(state0 == b.LOW && new_state == b.HIGH) {
time0 = new Date().getTime();
}
state0 = new_state;
var new_state = AnalogToDigital(LDR1, state1)
if(state == b.LOW && new_state == b.HIGH) {
time1 = new Date().getTime();
time2 = (time1 - time0);
console.log("Time passed: (s)");
console.log(time2);
}
state1 = new_state;
}
I've tried to do some research to see what I could change but am having no luck. Hopefully I can find some answers here. Thanks in advance!
EDIT (25, July): It has come to my attention that I did not state my question. Is there a way to use my LDRs on my BBB to measure the passing of time like how I could on my Arduino? Would you recommend a different IDE to use? Thank you!
EDIT (25, July): I came up with this code that allows me to somewhat measure the time on my BBB:
var b = require('bonescript');
var LDR = 'P9_38';
var LDR1 = 'P9_40';
var startTime;
var endTime;
var loopTimer = 1500;
const THRESHOLD = 590;
function start() {
var light = b.analogRead(LDR) * 1000;
if(light < THRESHOLD) {
startTime = new Date();
console.log(light);
}
}
function end() {
var light1 = b.analogRead(LDR1) * 1000;
if(light1 < THRESHOLD) {
endTime = new Date();
console.log(light1);
var timeDiff = endTime - startTime;
timeDiff /= 1000;
var seconds = (timeDiff).toFixed(2);
console.log('time passed in seconds: ');
console.log(seconds);
}
}
function loop() {
start();
end();
}
var timer = setInterval(loop, loopTimer);
It doesn't seem to like to measure time under ~1.5 seconds, though. I'll keep tinkering with it!
EDIT (26, July): I figured out my own solution! Here is the code:
var b = require('bonescript');
var LDR = 'P9_38';
var LDR1 = 'P9_40';
var startTime;
var endTime;
var loopTimer = 250;
const THRESHOLD_LDR = 700;
const THRESHOLD_LDR1 = 730;
function start() {
var light = b.analogRead(LDR) * 1000;
if(light < THRESHOLD_LDR) {
startTime = new Date();
console.log(light);
}
}
function end() {
var light1 = b.analogRead(LDR1) * 1000;
if(light1 < THRESHOLD_LDR1) {
endTime = new Date();
console.log(light1);
var timeDiff = endTime - startTime;
timeDiff /= 1000;
var seconds = (timeDiff).toFixed(2);
console.log('time passed in seconds: ');
console.log(seconds);
}
}
function loop() {
start();
end();
}
var timer = setInterval(loop, loopTimer);
Upvotes: 0
Views: 217
Reputation: 1594
On platforms like Arduino, you can easily predict the accurate timing of each operation that your code does(if you use assembly). This is because the arduino programming is literally bare bone, and right no the microprocessor that has nothing like cache, pipelines to produce lag or add unpredictability etc.
On the other hand, BeagleBone black runs a Linux based OS on top of it. If the OS is general purpose (not realtimeOS), its almost impossible to claim the accurate timing of your code. You see, OS has many things like scheduler and buffers. And the processor is also very much more complex, having cache and pipelines on its I/O pins. All this add more to the unpredictability.
A time constrained program is never good on a general purpose OS. They cant just handle it. There is where a Realtime OS kicks in.
For the BeagleBone black boards, we have something called PRU (Programmable Real Time Units). These are kinds of extra set of processors that are meant for time constrained operations. You can find more about them here.
Upvotes: 0