Arduino LED homework help !

Greeting

I have to write a program for homework or do not know how but whether we can help someone?

Task

Write a program in the Arduino language to the digital output of the microcontroller (pin 13) to which a LED send impulses continuously displayed (duration low or high voltage level = 0.8 s)!

Excuse my English :slight_smile:

Thanks

  1. switch led off, delay 100ms
  2. switch led on, delay 100ms
    2, 3, 4) switch led off, delay 300ms

I guess you can figure out the last 3

Place in loop() if it needs to be repeated, else in setup()

Take a look at the example called "Blink" in the IDE programming window. File>Examples>01Basics>Blink

If you are learning the C++ language and programming the Arduino , it us very useful to look through the example programs and to use the reference page to get information about the commands.

Hi,

Task

Write a program in the Arduino language to the digital output of the microcontroller (pin 13) to which a LED send impulses continuously displayed (duration low or high voltage level = 0.8 s)!

Have you been taught to program a microcontroller?

Tom...... :slight_smile:

sterretje:
0) switch led off, delay 100ms

  1. switch led on, delay 100ms
    2, 3, 4) switch led off, delay 300ms

But let's hope an as-yet unrevealed Part2 of the task isn't to read a switch in there somewhere :slight_smile:

I did not mention the delay function, just said 'delay' :smiley:

Yes, I learned a little bit programmable

Do you like this program should look like ??

Do you like this program should look like ??

No I don't. :frowning:

Since you are just beginning, it is good to start off on the correct path. If you are going to participate in this forum and ask for advice (and hopefully later to give help to others), you must learn to post your code using the code tags so that it appears like it is below and people can easily copy it. To learn how to use the code tags </> Read the How to use the forum guide

byte led = 12;
void setup() {
  pinMode (12, OUTPUT);
}
void loop() {
  digitalWrite(led, LOW);
  delay(700);
  digitalWrite(led, HIGH);
  delay(700);
  digitalWrite(led, LOW);
  delay(2100); //3x700ms
  digitalWrite(led, HIGH);
  delay(700);
  digitalWrite(led, LOW);
  delay(700);
  digitalWrite(led, HIGH);
  delay(700);
}

One thing I noticed, is that you didn't allow the compiler to do the math. You computed 3x700 and even commented it. You can say, delay(3*700) and let the compiler do the math.

The principle is: Whenever possible, say what you mean, and mean what you say.

sterretje:
I did not mention the delay function, just said 'delay' :smiley:

So how did that work out?

DenyX:
Do you like this program should look like ??

DenyX, before you go any further, and before you paint yourself into a corner, I would strongly urge you to read, digest and embrace the BlinkWithOutDelay approach shown here.

DenyX:
Do you like this program should look like ??

There is a lot to comment on. But everybody has to start somewhere.

I think your durations are a bit off. From the opening post, I understand that you have a total duration of 800ms; all your delays total to 5600ms.

But OK, you can now take it to the next level :wink:

As you can see, you do the same 'thing' time after time; switch LED on or off, wait a while, switch the LED on or off, wait a while, .....

This screams for the use of a function. Read up on them. A simple function could be

/*
  switch LED on or off for given time
*/
void doLed(int state, unsigned long duration)
{
  // set LED to given state
  digitalWrite(led, state);
  // delay for given duration
  delay(duration);
}

You can call this from loop() a number of times. Not perfect for the given homework as the approach should be a little different but it will do the job.

Next you might want to rethink the approach. You have a bit pattern that needs to be reflected by the LED (like in serial communications). So the duration in the above code is basically fixed (like the baudrate on a serial port).

The below is a slightly simpler function with a fixed duration of 100ms

void displayBit(int bitValue)
{
  // display bit
  digitalWrite(led, bitValue);
  // delay 100ms
  delay(100);
}

For every bit that you need to display, you call this function. So in the loop(), you call displayBit() 8 times with the appropriate bit value (LOW or HIGH, 0 or 1).

void loop()
{
  displayBit(LOW);   // display first bit
  displayBit(HIGH);
  displayBit(LOW);
  displayBit(LOW);
  displayBit(LOW);
  displayBit(HIGH);
  displayBit(LOW);
  displayBit(HIGH);  // display last bit
}

Note: this might be too advanced for your homework; so be careful what you submit :slight_smile:

There are a few more steps that you can take. E.g. using a byte to store the data pattern and looping through the bits (e.g. with a for loop) and call displayBit based on the bit in the data pattern.

A note on the delay (just to keep JimboZA happy :wink: ). The total cycle that you posted takes 5.6 seconds. What if you need to be able to interrupt the cycle at anytime using e.g. a button. You press the button and next you have to wait up to 5.6 seconds before the code can detect that the button was pressed.

The IDE comes with a BlinkWithoutDelay example; there is also a post here called several things at a time. Go through them, understand what they do and why they do what they do.