Hello, I've been hitting my head on this problem: I want to break a for loop when I meet a condition and restart it and while I'm able to break it using the command "break" I can't really make it restart automatically. Here is the code
const int RX = 3; //recieving pin
float RXValue[80];
float RXCodeChecker[80];
void setup() {
pinMode(RX, INPUT);
Serial.begin(9600);
//code checker for 01010101
for (int t = 0; t < 10; t++) {
RXCodeChecker[t] = 0;
}
for (int t = 10; t < 20; t++) {
RXCodeChecker[t] = 1;
}
for (int t = 20; t < 30; t++) {
RXCodeChecker[t] = 0;
}
for (int t = 30; t < 40; t++) {
RXCodeChecker[t] = 1;
}
for (int t = 40; t < 50; t++) {
RXCodeChecker[t] = 0;
}
for (int t = 50; t < 60; t++) {
RXCodeChecker[t] = 1;
}
for (int t = 60; t < 70; t++) {
RXCodeChecker[t] = 0;
}
for (int t = 70; t < 80; t++) {
RXCodeChecker[t] = 1;
}
}
void loop() {
// finds the duration of a signal
unsigned long duration = pulseIn(RX, HIGH);
if (duration > 40000) {
int bitChecked = 0;
for (int s = 0; s < 80; s++) {
RXValue[s] = 0.00;
}
// Verify code 01010101
if (digitalRead(RX) == LOW) {
// cycle for 80ms
for (int b = 0; b < 80; b++) {
RXValue[b] = digitalRead(RX);
// wait 999 Microsecond
delayMicroseconds(999);
}
//Check
for (int d = 0; d < 80; d++) {
if (RXValue[d] == RXCodeChecker[d]) {
bitChecked++;
}
}
if (bitChecked >= 70) {
Serial.print("Segnale");
Serial.println(" ");
}
}
}
}
I want to break
if (digitalRead(RX)== LOW ) {
// cycle for about 80ms
for (int b = 0; b < 80; b++) {
RXValue[b] = digitalRead(RX);
// wait 999 Microsecond
delayMicroseconds(999);
}
and restart it whenever I get another HIGH signal for 40ms or more.
I've tried modifying it as follows but it doesn't work
const int RX = 3; //recieving pin
float RXValue[80];
float RXCodeChecker[80];
bool highDetected = false;
int bitChecked;
void setup() {
pinMode(RX, INPUT);
Serial.begin(9600);
//code checker for 01010101
for (int t = 0; t < 10; t++) {
RXCodeChecker[t] = 0;
}
for (int t = 10; t < 20; t++) {
RXCodeChecker[t] = 1;
}
for (int t = 20; t < 30; t++) {
RXCodeChecker[t] = 0;
}
for (int t = 30; t < 40; t++) {
RXCodeChecker[t] = 1;
}
for (int t = 40; t < 50; t++) {
RXCodeChecker[t] = 0;
}
for (int t = 50; t < 60; t++) {
RXCodeChecker[t] = 1;
}
for (int t = 60; t < 70; t++) {
RXCodeChecker[t] = 0;
}
for (int t = 70; t < 80; t++) {
RXCodeChecker[t] = 1;
}
}
void loop() {
// finds the duration of a signal
unsigned long duration = pulseIn(RX, HIGH);
if (duration > 40000) {
int bitChecked = 0;
for (int s = 0; s < 80; s++) {
RXValue[s] = 0.00;
highDetected = true;
}
}
// Verify code 01010101
if (digitalRead(RX) == LOW && highDetected) {
// Ciclo per 80 millisecondi
for (int b = 0; b < 80; b++) {
RXValue[b] = digitalRead(RX);
// wait 999 Microsecond
delayMicroseconds(999);
}
highDetected = false;
//Checking
for (int d = 0; d < 80; d++) {
if (RXValue[d] == RXCodeChecker[d]) {
bitChecked++;
}
}
if (bitChecked >= 75) {
Serial.print("Signal");
Serial.println(" ");
}
while (highDetected) {
if (duration > 40000) {
highDetected = true;
break;
}
}
}
}
Edit: the problem with this last code is that I get the signal outcome two times in fast succession even when I should get one
Do you mean pick up from where it left off as if you hadn't used break to exit the for loop early?
You might need to code the process yourself without using for loops.
Please describe what that code is trying to accomplish. There is no generic answer to your question, and solving your problem will need knowing what you are trying to do.
We could reverse engineer a specification from the code, but since the code doesn't yet do what you want that woukd be a potential waste of time.
Also, fix your code by applying the IDE Autoformat tool, just do that and edit your post pasting the formatted code all over what you did first time. It will be easier for everyone you too to read.
Thank you about the info on autoformat!
In the first code whenever i get a HIGH signal for 40ms or more a for loop starts where it takes the value of said signal every ms and then compere this values to another array. However there are times where i get a "fake" high signal due to interference and in this case if the true signal is during the for loop i wrote it doesn't register because arduino is taking the values of the first signal. What I'm trying to accomplish is break this for loop that is registering the values and start it again whenever I get a new HIGH signal for more than 40ms. I hope I explaind myself, english is not my first language
while (highDetected) {
if (duration > 40000) {
highDetected = true;
break;
}
If "highDetected" is false, does nothing, but if it's true the code enters the while() where if duration is less than 40000 does nothing (and "duration" doesn't change anywhere here), so it's an infinite loop, and if it is greater than 40000 the code immediately exits the while, doing nothing except setting "highDetected" to "true".
Why is RX value a float is all you're ever going to store there is 0 or 1? You know you can't compare floats with == and expect them to act right. Floating point variables are inherently inaccurate.
If a variable is only ever going to hold 0 or 1 then it should be bool, or at most byte.
It appears as if you are doing a very elaborate variation of a simple pattern recognition.
It is unlikely that you will ever match the input by examining it in the manner you are trying to code.
Please draw a timing diagram of the signal you are trying to recognize, and specific the accuracy with which you expect it to be arriving.
This might better be sampled the way UART does it, have you looked into how regular serial data is recovered from the serial stream?
A primitive version samples in the middle of a bit after figuring out that a character is coming along.
More elaborate methods sample several times during a bit period.
If I understand, you are also dealing with noise of some kind, here a more involved version of switch debouncing can be employed, which woukd better be called deglitching, which allows a low level high er speed sampler to step right over any brief excursions that are not really part of the data.
But diagrams or more words about what the signal is, and what you want to derive from an analysis of that signal.
I'm trying to recognize these patterns that I'm sending from various transmitters to a single cheap OOK receiver connected to Arduino. I know I could use some libraries like RadioHead, but from my understanding, these libraries work by sending a wave continuously with a fixed frequency and shift the frequency when they have to send data. This method wouldn't work with multiple transmitters.
I've tried to reduce the noise caused by these receivers using low-pass filters and the ResponsiveAnalogRead library, but these methods did not work well enough (they introduced delays and fake signals).
My solution was to send an 8-bit 1kHz wave to adjust the Automatic Gain Control of the receiver and then send a simple serial code. This way, I could also know which transmitter I was sending data from. The 01010101 serial code in my script is just an example.
The code I used was written to overcome two other problems of my receiver: the first 8-bit HIGH wave is not immediately received. Usually, the receiver doesn't receive the first 2/3-bits, and the receiver sometimes slightly shifts the bits of my serial code, creating an uneven duty cycle.
Edit: The picture I'have sent is a week old, I slightly changed the transmitted wave and now the 8-bit wave before the serial code is consistently high once it gets high.
This might better be sampled the way UART does it, have you looked into how regular serial data is recovered from the serial stream?
I have not. I will look into that now, thank you very much.
I have to focus on something else right now, but thanks for the picture and words I will give them the attention they deserve.
Putting this in context is very helpful.
I have done 433 from the bottom up. If you have not heard of Manchester encoding I suggest to you that it would be worth looking at.
It is ideal in this case as it gets along very well with the data slicer in the receiver as it has an average value of 50 % over even a relatively short term
You can train the receiver with a longish stream of zeros, which allows synchronisation of a data recovery algorithm as well. Then you can send data, however you want to dress it up, without regard to its DC balance.
There may be libraries other the RadioHesd that are all over this. I prefer to use code I've had, and prefer to figure things out down to the metal, so to speak. It is where I find fun. YMMV, as ,might you idea of what is fun.
ALWAYS say that. Never call yourself stupid. But ignorance you should always admit. You shouldn't be ashamed to admit that you haven't learned a thing yet. The most dangerous thing is to pretend you have.
I don't see why not. If you have multiple transmitters, you'll have to implement some kind of sharing on the channel.
433 in some parts of the world is meant for relatively low rate periodic transmission of small amounts of data.
So each transmitter would chirp and delay by a pseudorandom time, rinse and repeat.
An analysis beyond me would be to determine the average time it would take to have a good chance of having received a message. My time would be better spent writing a program to simulate the entire system and just see how it does.
Since this is open loop. At the receiver, you could have a status indication per transmitter.
As an example, take a system meant to detect water leaks all over the place. For each instance of a detector, a green lamp would mean a recent "dry" message is arrived, a red lamp could be a sticky mode meaning a "wet" message has been received and an amber lamp might mean no message has successfully been decoded from the corresponding unit after the time one should have: an indication of failure, including in failure that the battery might have been drained.