My "Strobe" sketch.

Folks,

Sorry it has been a while for me writing sketches.

This is my recent effort to make a programmable STROBE.

For now it pulses pin 13.

I enter the frequency via the serial port/USB.

The lower part of the code is where the frequency is "made".

I put the "if frequency >1" because (obviously) you can't have anything less than 1.

But with that, nothing happens.

If I remove that condition it blinks the LED.

Yeah, I am missing something obvious, but it illudes me.

Any one - please?

#define outputpin 13
String Sfrequency = "";
int frequency;
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
}
void loop()
{
  // put your main code here, to run repeatedly:
  while (Serial.available() > 0)
  {
    //
    int newchar = Serial.read();
    //Serial.println("This is a test");
    //Serial.print("{test line ");
    //Serial.print(newchar);
    //Serial.println(" }");
    if (isDigit(newchar))
    {
      //
      Sfrequency += (char)newchar;
    }
    if (newchar == '\n')
    {
      //
      Serial.println("frequency");
      Serial.println(Sfrequency);
      Serial.println("Value");
      Serial.println(Sfrequency.toInt());
      frequency = Sfrequency.toInt();
      Serial.println(frequency);
      Sfrequency = "";
    }
  }

  if (frequency > 1)
  {
    digitalWrite(outputpin, HIGH);
    delay(1000/(2*frequency));
    digitalWrite(outputpin, LOW);
    delay(1000/(2*frequency));
    //Serial.println(" Done ");  
  }
  

}

because (obviously) you can't have anything less than 1.

So, 0.2Hz (200mHz) isn't less than one? :wink:

Ok, AWOL,

If I entered 0 (as I did once by mistake) it kinda pooped its pants.

Also entering 0 would suffice to stop it outputing the signal.

If I entered 0 (as I did once by mistake) it kinda pooped its pants.

Because twice zero is still zero, and you should never divide anything by zero.

delay(1000/(2*frequency));

Yes, and that is why I included the line:

if (frequency >1)

So I enter 25 and nothing happens.

I learned that I had to change the window to send "newline" to make it work.

So I am confused. (Gee I wonder why I picked the nic' name huh?)

This is what I get in the window when I am "talking" to the arduino:
frequency
10
Value
10
10
frequency
50
Value
50
50
frequency
20
Value
20
20

So it seems to be getting the number/s. So why isn't it acting on it?

So why isn't it acting on it?

Maybe the flicker is too fast for you to see.
Why not ">= 1" ?

At some iteration of the sketch I don't think I had the conditional "if frequency >1" set.

It "worked" in that the LED would change blink speed depending on the number.

Putting in 5 it would be slow.

2 would be really slow.

I did try numbers less than 10 and greater than 1.

It seems to show the numbers but not act on them.

The line:

frequency = Sfrequency.toInt();

That gets the "string" of Sfrequency and makes it into an "INT" - right?

(I'm getting these commands from other sketches to do with comms and serial.read etc.)

So in the sketch:
Serial.println("frequency");
Serial.println(Sfrequency);
Serial.println("Value");
Serial.println(Sfrequency.toInt());
frequency = Sfrequency.toInt();
Serial.println(frequency);

It prints "frequency"
Then the string value of Sfrequency.
Then "Value"
Then the int value of Sfrequency.

So why is it that if "Sfrequency" is 50 (as in text) and that is printed, why does it print another 50 which is an integer?
Isn't that line - Serial.println(frequency); - a bit like the old basic command: print chr$(frequency)?

frequency
50 <-- This is char()
Value
50 <-- This is int()
50 <-- As above.

So why is it that if "Sfrequency" is 50 (as in text) and that is printed, why does it print another 50 which is an integer?

because

Serial.println(Sfrequency.toInt());
      frequency = Sfrequency.toInt();
      Serial.println(frequency);

I really don't see why you're surprised when a sketch does what you have told it to do.

At some iteration of the sketch I don't think I had the conditional "if frequency >1" set.

I don't understand that sentence.

It "worked" in that the LED would change blink speed depending on the number.

So what's the problem?

I think that I had the "simple sketch" and I could sent numbers like 10, 4, 200, 80, 14, and the LED would change frequency.

I didn't get to actually checking the frequency, and put in 0 by mistake once and it crapped itself.

So I added the condition:

if (frequency >1)
{

}

around the actual bottom block.

Then - I think - is when it just simply stopped blinking the LED, but replying to my inputs.

Update:

If I modify the sketch to:

#define outputpin 13
String Sfrequency = "";
int frequency;
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
}
void loop()
{
  // put your main code here, to run repeatedly:
  while (Serial.available() > 0)
  {
    //
    int newchar = Serial.read();
    //Serial.println("This is a test");
    //Serial.print("{test line ");
    //Serial.print(newchar);
    //Serial.println(" }");
    if (isDigit(newchar))
    {
      //
      Sfrequency += (char)newchar;
    }
    if (newchar == '\n')
    {
      //
      Serial.println("frequency");
      Serial.println(Sfrequency);
      Serial.println("Value");
      Serial.println(Sfrequency.toInt());
      frequency = Sfrequency.toInt();
      Serial.println(frequency);
      Sfrequency = "";
    }
  }

//  if (frequency > 1)
//  {
    digitalWrite(outputpin, HIGH);
    delay(1000/(2*frequency));
    digitalWrite(outputpin, LOW);
    delay(1000/(2*frequency));
    //Serial.println(" Done ");  
//  }
  

}

I don't get the reply when I enter numbers.

But (and I think I've already asked this question) why not ">= 1" ?

Maybe the LED is blown for some other reason.
It would be very simple to replace the LED code with some simple serial prints.

, and put in 0 by mistake once and it crapped itself.

I have only once in my career seen hardware damage caused directly by incorrect/ill-advised software.

int frequency;
 delay(1000/(2*frequency));

Remember what I said about twice zero still being zero?

AWOL,

I didn't mean to imply the arduino was damaged with I entered the 0 as frequency.
I simply thought I would make it that if I entered 0, I could stop the output.

Line changed to >= 1.

Same result.

I get the reply in the window, but nothing happens.

The LED isn't blown - compiling BLINK, it flashes quite happily.

Yes, I also understand about the /ZERO problem.

That is also why I have the "if (frequency >= 1)" line in there to stop it happening.
That is: The /ZERO error.

I don't get the reply when I enter numbers.

I'm completely confused about what your problem is now.
You don't get a reply because your code is stuck in a massive delay.

That's one of the reasons we try to encourage you not to use "delay".

Ok,

Thanks.

I think I get you.

It isn't that I am not listening, but I would like to explain my rational to what I am doing.

Giving a bit of latitude for things to happen:

delay(1000);

Is a ONE SECOND delay.

So if I have a frequency of 1 Hz, it has to turn ON/OFF every second.

Frequency = 1.

It has to turn the LED on for half a second and off for half a second.

So:

delay(1000/(frequency*2));

That would give:
1000/(1*2)
1000/2
500
So delay(500)
If I enter a higher frequency - up to 1kHz - the delay would go down to 1.

So yes, if frequency = 0, then 1000/0 would be problematic.

I nearly thought that at boot up, it would lock up, but if the line:
if (frequency >=1) is there, it would prevent the /ZERO error happening.
Only when the frequency is greater than 1, does that part of the code happen.

I put this bit in the code:

    if (newchar == '\n')
    {
      //
      Serial.println("frequency");
      Serial.println(Sfrequency);
      Serial.println("Value");
      Serial.println(Sfrequency.toInt());
      frequency = Sfrequency.toInt();
      Serial.println(frequency);
      Sfrequency = "";
    }
  }
//  These two lines
  Serial.println("test");
  Serial.println(frequency);

  if (frequency >= 1)
  {
    digitalWrite(outputpin, HIGH);
    delay(1000/(2*frequency));
    digitalWrite(outputpin, LOW);
    delay(1000/(2*frequency));
    //Serial.println(" Done ");  
  }

If I enter 1, I see a slow test scrolling down the window.
If I enter 2 it scrolls (nearly, though I would say IS) twice as fast.

So that part works.

So, the frequency value is set - isn't it?
Why is it not seeming to get into that part of the code to turn the LED on and off?

I'm sorry, I genuinely do not see what the problem is here.
This code works exactly as I would expect it to - for numbers above about 30, it is difficult to see the flicker in a bright environment, but my scope says it is there.

#define outputpin 13
String Sfrequency = "";
int frequency;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  // put your main code here, to run repeatedly:
  while (Serial.available() > 0) {
    int newchar = Serial.read();
    if (isDigit(newchar)) {
      Sfrequency += (char)newchar;
    }
    if (newchar == '\n') {
      Serial.println("frequency");
      Serial.println(Sfrequency);
      Serial.println("Value");
      Serial.println(Sfrequency.toInt());
      frequency = Sfrequency.toInt();
      Serial.println(frequency);
      Sfrequency = "";
    }
  }

  if (frequency >= 1) {
    digitalWrite(outputpin, HIGH);
    delay(1000/(2*frequency));
    digitalWrite(outputpin, LOW);
    delay(1000/(2*frequency));
  }  
}

Weird.

It doesn't work for me.

BLINK works. The LED blinks.

I load that sketch and enter numbers like 2, 3, 10 and low number and get ZIP/NADA/ZILCH.

What line-ending have you got set in the serial monitor?

Newline.

I fell for that trap and have learned the lesson. :wink:

And now for some reason it works.

Go figure.