I don’t think I did. Do you have a link ?
Sorry, I can’t help with that.
I don’t think I did. Do you have a link ?
Sorry, I can’t help with that.
I appreciate your kindness. Please forgive my poor English!
I’m completely out of options. I need your help…
The sensors are connected with appropriate resistors in series: 5V to the LED and 3.3V to the sensor. Currently, as you advised, I’ve connected the sensors to Data3 and Data4, and I plan to use TX and RX for the motor.
I’m using `analogRead` to detect those subtle differences in the readings and fine-tune the timing for stopping the blades from extending or retracting.
For some reason, the sensor always returns 0, and it’s not working properly.
The code is as follows.
class BladeExtender : public Looper {
public:
const int extendSensorPin = 0; // Data4
const int retractSensorPin = 17; // Data3
const int motorAPin = 8; //RX
const int motorBPin = 9; //TX
const char* name() override { return "BladeExtender"; }
void Setup() override {
Serial.begin(9600);
}
void Loop() override {
if (on_ != SaberBase::IsOn()) {
on_ = SaberBase::IsOn();
if (on_) {
EnableBooster();
Serial.println("######Change extending_#######");
Serial.println(analogRead(retractSensorPin));
extending_ = true;
retracting_ = false;
} else {
EnableBooster();
Serial.println("######Change retracting_#######");
Serial.println(analogRead(retractSensorPin));
retracting_ = true;
extending_ = false;
}
wait_time_millis_ = millis();
}
if (SaberBase::IsOn()) {
if (extending_) {
//Serial.println(analogRead(retractSensorPin));
//Drive the motor as long as the sensorA returns a value of 1,000 or less
if (analogRead(retractSensorPin) <= 1000) {
Serial.print("#####Extending##### ");
analogWrite(motorAPin, 255);
analogWrite(motorBPin, 0);
//The sensorA count has exceeded 1,000, and the brakes haven't been applied yet.
} else if (! braked_) {
//To verify this in conjunction with the actual blade elongation, check the actual values here.
Serial.println("#####Extend Stop#####");
Serial.println(analogRead(retractSensorPin));
analogWrite(motorAPin, 255);
analogWrite(motorBPin, 255);
braked_ = true;
//Record the time the brakes were applied
last_brake_time_millis_ = millis();
//Release the brakes after 50 m/s.
} else if (millis() - last_brake_time_millis_ > 50) {
analogWrite(motorAPin, 0);
analogWrite(motorBPin, 0);
extending_ = false;
braked_=false;
//Consume a loop that does nothing until the time comes
} else {
// do nothing
}
} else if (retracting_) {
//Serial.println(analogRead(retractSensorPin));
//Keep the motor running until the sensorB returns a value of 1,005 or higher
if (analogRead(retractSensorPin) <= 1005) {
//Serial.print("#####Retracting#####");
analogWrite(motorAPin, 0);
analogWrite(motorBPin, 255);
//The sensorB count has exceeded 1,000, and the brakes haven't been applied yet.
} else if (! braked_) {
//To verify this in conjunction with the actual blade elongation, check the actual values here.
Serial.println("#####Retract Stop#####");
analogWrite(motorAPin, 255);
analogWrite(motorBPin, 255);
braked_ = true;
//Record the time the brakes were applied
last_brake_time_millis_ = millis();
//Release the brakes after 50 m/s.
} else if (millis() - last_brake_time_millis_ > 50) {
analogWrite(motorAPin, 0);
analogWrite(motorBPin, 0);
retracting_ = false;
braked_=false;
//Consume a loop that does nothing until the time comes
} else {
// do nothing
}
}
}
}
private:
// Any variables you need go here.
bool extending\_ = false;
bool retracting\_ = false;
bool on\_ = false;
bool braked\_ = false;
uint32_t wait_time_millis\_;
uint32_t last_brake_time_millis\_;
};
// None of the above does anything until you instantiate the class like this:
BladeExtender blade_extender;
Only some pins support analogRead().
On a V2 board, that’s only Data1 and Data3.
On a V3 board Data1, Data4, Free3 (pin 10), Button 1 (12), RX and TX.
Thank you sooooo much!
Well, since V3 hasn’t arrived in Japan yet, I’ll try connecting the sensors to Data1 and Data3 for now.
As for the motor driver, it should work with `digitalWrite`.
I’ll let you know the results later!!
it could.
The OS shouldn’t do anything with these pins normally, but if you have something in your config file that uses these pins, that would cause problems, this could be a button, a blade, or a define like ENABLE_SERIAL. If you post your config file, I can check if something would be interfering with these pins.
Have you checked the voltages on the signal pin?
I should also point out that I normally don’t use anlogRead in ProffieOS code, but LSAnalogRead. LSAnalogRead is a class which separates setup, start and read operations, which makes it slightly harder to use, but significanly faster. I would recommend using LSAanlogRead over analogRead(), however, I think analogRead() should work just fine…
Hello!!
We continued testing for dozens of hours with V3.9 board.
First, the motor driver operated using the TR and RX pins!!
EnableBooster is functioning properly, the LBR-127HLD’s infrared LED is emitting light normally, and the phototransistor’s power supply is connected to the 3.3V pin.
However, even when the phototransistor’s signal line is connected to Data1, Data4, and Free3—as you recommended—it always returns a value of 0, regardless of whether there is an object to reflect off or the distance to it…
As for Button 1, it is returning numbers ranging from 0 to 20 erratically.
Since I’ve used this sensor many times before, it’s not a wiring error.
The overall program control is functioning normally, so the only remaining issue is that the `analogRead` for the photoreflector is not working properly.
Could this be due to some kind of software interference within the OS or a reset?
A few more things to check:
The sensor I’m using and its intended application simply won’t work unless I use `analogRead`.
I use this to monitor the subtle adjustments needed to reach the end points of the blade’s extension and retraction.
I’ve connected a 220Ω resistor to the LED’s power supply and a 1kΩ resistor to both the sensor’s power supply and signal line.
You can use analogRead, but don’t use analogWrite unless you need it.
Is that 5V? The analog inputs are NOT 5v-tolerant, and you may have burnt something if you give it 5 volts. Analog inputs should not have more than 3.3 volts.
What is the actual voltage on the input if you measure it?
I am not using `analogWrite`.
The motor driver is already working properly with `digitalWrite`.
Only the sensor’s “infrared emitting LED” is powered by 5V, while the receiver is powered by 3.3V.
The last code pasted above has analogWrite in it.
If you’re not using it anymore, that’s good.
Good. So what’s the voltage when you’re calling analogRead() ?
Also, did you try adding the pinMode() calls?
Here is the current code.
class BladeExtender : public Looper {
public:
const int extendSensorPin = blade7Pin;
const int retractSensorPin = blade4Pin;
const int motorAPin = rxPin;
const int motorBPin = txPin;
const int extendLimit = 1000;
const int retractLimit = 1005;
const char* name() override { return "BladeExtender"; }
void Setup() override {
pinMode(extendSensorPin, INPUT);
pinMode(retractSensorPin, INPUT);
pinMode(motorAPin, OUTPUT);
pinMode(motorBPin, OUTPUT);
}
void Loop() override {
if (on_ != SaberBase::IsOn()) {
on_ = SaberBase::IsOn();
EnableBooster();
if (on_) {
value=analogRead(extendSensorPin);
if (value <= extendLimit) {
Serial.print("######Start extend####### ");
Serial.println(value);
extending_ = true;
retracting_ = false;
digitalWrite(motorAPin, HIGH);
digitalWrite(motorBPin, LOW);
start_time_millis_= millis() + 3600;
} else {
Serial.println("######LOW####### ");
}
} else {
value=analogRead(retractSensorPin);
if (value <= retractLimit) {
Serial.println("######Start retract####### ");
Serial.println(value);
retracting_ = true;
extending_ = false;
digitalWrite(motorAPin, LOW);
digitalWrite(motorBPin, HIGH);
start_time_millis_= millis()+ 3600;
} else {
Serial.println("######LOW####### ");
}
}
}
if (extending_) {
value=analogRead(extendSensorPin);
if ((millis() > start_time_millis_) || (value > extendLimit)) {
Serial.println("#####Hit the Brakes##### ");
digitalWrite(motorAPin, LOW);
digitalWrite(motorBPin, LOW);
extending_ = false;
retracting_ = false;
} else {
Serial.println("#####Do Nothing##### ");
}
} else if (retracting_) {
value=analogRead(retractSensorPin);
if ((millis() > start_time_millis_) || value > retractLimit)) {
Serial.println("#####Hit the Brakes##### ");
digitalWrite(motorAPin, LOW);
digitalWrite(motorBPin, LOW);
extending_ = false;
retracting_ = false;
} else {
Serial.println("#####Do Nothing##### ");
}
}
}
private:
bool on\_ = false;
bool extending\_ = false;
bool retracting\_ = false;
int value;
uint32_t start_time_millis\_;
};
BladeExtender blade_extender;
Looks pretty reasonable… wait…
Is this running on a V3 board?
The free3 pad is analog capable, but not using “blade7Pin”, instead you need to use the number 10. (The free3 pad is connected to two separate pins on the CPU.)
The “pin function” table on this page shows what each pin/pad is capable of: Proffieboard V3
Yes!! following your advice, the V3 board I ordered arrived,
and I’ve been working with it since yesterday.
I’ve already set up the motor drivers using digitalWrite on the TX and RX pins,
so my only remaining challenge is finding two pins where I can read values using analogRead.
The other day, you recommended Data1, Data4, Free3, and Button 1 among the V3 board’s pins.
Which two would you recommend as the best options?
I’ve also been experimenting with physical limit switches. While it seems possible to determine when the blade has fully retracted into the emitter section during blade retraction, it’s difficult to determine when the blade has fully extended using a limit switch.
It seems like it might work if I punch a hole at the base of the blade material and check whether the switch makes contact, but when I actually try it, the switch gets caught in the hole.
As expected, it’s best to use a reflective photosensor as before.
I changed the pin declaration to 10, but it still only returns 0. Actually, last night, when I temporarily attached a physical limit switch and controlled it using `digitalRead`, it worked fine on the same pin. Only `analogRead` isn’t working.
class BladeExtender : public Looper {
public:
const int extendSensorPin = blade4Pin;
const int retractSensorPin = 10;
const int motorAPin = rxPin;
const int motorBPin = txPin;
const int extendLimit = 1000;
const int retractLimit = 1005;
const char* name() override { return "BladeExtender"; }
void Setup() override {
pinMode(extendSensorPin, INPUT);
pinMode(retractSensorPin, INPUT);
pinMode(motorAPin, OUTPUT);
pinMode(motorBPin, OUTPUT);
}
void Loop() override {
if (on_ != SaberBase::IsOn()) {
on_ = SaberBase::IsOn();
EnableBooster();
if (on_) {
value=analogRead(retractSensorPin);
I think I figured out what is going on.
The battery monitor code is doing an asynchronous analogRead, which makes the regular Arduino analogRead not work.
The solution is to do this:
struct AnalogReadHelper : public AnalogRead {
explicit AnalogReadHelper(int pin) : AnalogRead(pin) {}
void Setup() override {
value_ = -1;
AnalogRead::Setup();
Start();
}
void loop() override {
if (Done()) Start();
AnalogRead::loop();
}
};
AnalogReadHelper extendReader(blade4pin);
AnalogReadHelper retractReader(10);
Then, in your class, you call extendReader.Value() instead of analogRead(extendSensorPin). Note that you’ll get -1 the first few times, so you might need to ignore that. After that, it will return the “latest” value every time you call it.
The AnalogRead class has code that makes sure that only one read operation is running at a time, so the battery monitor and the two AnalogReadHelper instances will take turns.
I’m glad it looks like I’ve found a solution. Thank you very very much!
Where exactly in which file and on which line should I add this code?
Right before the BladeExtender class should work.