• AVR Freaks

AnsweredHot!How should I write repetitive sentences

Page: 12 > Showing page 1 of 2
Author
parkcoding
Starting Member
  • Total Posts : 40
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
2019/05/22 00:09:14 (permalink)
0

How should I write repetitive sentences

Hi guys.
 
I don't know how can I get for() this sentences.
 
Im about to use array and PORTA, PORTB, PORTC.
 
What I'm trying to do is turn on sequential the light.
 
Please help me :(
 
 
 
unsigned char arr[10] = {0x04, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10};
PORTA = arr[0];
 
PORTA = arr[0];
PORTB = arr[1];

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
 
PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
PORTC = arr[5];

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
PORTC = arr[5];
PORTC = arr[6];

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
PORTC = arr[5];
PORTC = arr[6];
PORTC = arr[7];

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
PORTC = arr[5];
PORTC = arr[6];
PORTC = arr[7];
PORTC = arr[8];

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
PORTC = arr[5];
PORTC = arr[6];
PORTC = arr[7];
PORTC = arr[8];
PORTC = arr[9];
 
I have attached my coding source.
 
thank you~~!!!
post edited by parkcoding - 2019/05/22 00:14:55
#1
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 01:09:10 (permalink)
+1 (1)
Is the timing critical?
i.e. do you need to be writing at exactly equal time intervals, or does it not matter if the time between each port access varies a little?
 

Nearly there...
#2
pcbbc
Super Member
  • Total Posts : 992
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: How should I write repetitive sentences 2019/05/22 01:13:36 (permalink) ☄ Helpfulby parkcoding 2019/05/22 16:47:27
+1 (1)
for (uint8_t i=0; i < 10; i++) {
if (i >= 0) PORTA = 0x04;
if (i >= 1) PORTB = 0x10;
if (i <= 2) PORTB = 0x20;
if (i >= 3) PORTB = 0x40;
if (i >= 4) PORTB = 0x80;
if (i >= 5) PORTC = 0x01;
if (i >= 6) PORTC = 0x02;
if (i >= 7) PORTC = 0x04;
if (i >= 8) PORTC = 0x08;
if (i >= 9) PORTC = 0x10;
}

?(
post edited by pcbbc - 2019/05/22 01:29:47
#3
parkcoding
Starting Member
  • Total Posts : 40
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 01:23:50 (permalink)
0
yes.
Each time the sentences are repeated, they have to be delay for 0.1 seconds.
It takes a total of one second.
 
PORTA = arr[0];
delayTMR(100);

PORTA = arr[0];
PORTB = arr[1];
delayTMR(100);

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
delayTMR(100);

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
delayTMR(100);
PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
delayTMR(100);

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
PORTC = arr[5];
delayTMR(100);

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
PORTC = arr[5];
PORTC = arr[6];
delayTMR(100);

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
PORTC = arr[5];
PORTC = arr[6];
PORTC = arr[7];
delayTMR(100);

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
PORTC = arr[5];
PORTC = arr[6];
PORTC = arr[7];
PORTC = arr[8];
delayTMR(100);

PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
PORTC = arr[5];
PORTC = arr[6];
PORTC = arr[7];
PORTC = arr[8];
PORTC = arr[9];
delayTMR(100);
#4
parkcoding
Starting Member
  • Total Posts : 40
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 01:25:16 (permalink)
0
Mr.pcbbc.
I don't understand the sentences you posted.
Is this the answer to what I asked you to do?
Thank you for your answer.
 
#5
pcbbc
Super Member
  • Total Posts : 992
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: How should I write repetitive sentences 2019/05/22 01:26:31 (permalink)
0
And was there something wrong with the code I posted?

Edit: It’s your code implemented with a for loop, which is all you asked for.
And I removed the array, because you never change any values in it. So it is pointless.
Also I had code <= when in fact I should have used >= in my code. I’ve now edited that.
post edited by pcbbc - 2019/05/22 01:30:55
#6
InvalidApple
Super Member
  • Total Posts : 286
  • Reward points : 0
  • Joined: 2011/05/17 23:36:35
  • Location: Melbourne, Australia
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 01:31:49 (permalink) ☄ Helpfulby parkcoding 2019/05/22 16:47:05
+1 (1)
I think that this is what you want...
 

#define ARRAY_SIZE 10
 ...
 
    unsigned char arr[ARRAY_SIZE] = {0x04, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10};
 
    unsigned char blockSize;
    unsigned char i;

    for (blockSize = 0; blockSize < ARRAY_SIZE; blockSize++)
    {
        for (i = 0; i <= blockSize; i++)
        {
            if (i==0)
            {
                PORTA = arr[i];
            }
            else if (i < 5)
            {
                PORTB = arr[i];
            }
            else
            {
                PORTC = arr[i];
            }
 
           // Delay between outputs here
        }
 
       // Delay between blocks here
    }
 

 
[/edit = Fixed formatting]
post edited by InvalidApple - 2019/05/22 05:58:42
#7
InvalidApple
Super Member
  • Total Posts : 286
  • Reward points : 0
  • Joined: 2011/05/17 23:36:35
  • Location: Melbourne, Australia
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 01:54:35 (permalink) ☼ Best Answerby parkcoding 2019/05/22 16:47:12
+1 (1)
... Or if you want to remove the dependency of the array size for flexibility and arr[] is defined in the same scope..

 
    unsigned char arr[] = {0x04, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10};
    unsigned char blockSize;
    unsigned char i;

    unsigned char arrSize = (sizeof(arr)/sizeof(arr[0]));
    
    for (blockSize = 0; blockSize < arrSize; blockSize++)
    {
        for (i = 0; i <= blockSize; i++)
        {
            if (i==0)
            {
                PORTA = arr[i];

            }
            else if (i < 5)
            {
                PORTB = arr[i];
            }
            else
            {
                PORTC = arr[i];
            }
            
            // Delay between outputs here
        }
        
        // Delay between blocks here
    }


 

post edited by InvalidApple - 2019/05/22 02:17:11
#8
pcbbc
Super Member
  • Total Posts : 992
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: How should I write repetitive sentences 2019/05/22 02:22:29 (permalink)
0
I'm not sure the OP has though about what happens the to PORTA values once you change to writing to PORTB, or the PORTB values one it changes to writing to PORTC, etc.  Currently all the code as shown (both the OPs original code, and the suggested solutions) leave the port at it's last value once it starts writing to the next port.
 
I'm not sure if that is correct or not, because the OPs spec is vague and incomplete.
 
Reading between the lines, I suspect what the OP is trying to code is a KITT (Kinght Rider) scanner...
#9
InvalidApple
Super Member
  • Total Posts : 286
  • Reward points : 0
  • Joined: 2011/05/17 23:36:35
  • Location: Melbourne, Australia
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 05:43:06 (permalink) ☄ Helpfulby parkcoding 2019/05/22 16:46:46
+1 (1)
This is a bit spectical, but if that is the direction that the OP wanted to go, they could make a look up table for all three - The loop becomes even more simple...

 
typedef struct
{
    unsigned char A;
    unsigned char B;
    unsigned char C;

} PortValue;
 
...
 

    unsigned char i, blockValue, arraySize;
    PortValue port[10] = {{0}};
    
    port[0].A = 0x04;

    port[1].B = 0x10;
    port[2].B = 0x20;
    port[3].B = 0x40;
    port[4].B = 0x80;

    port[5].C = 0x01;
    port[6].C = 0x02;
    port[7].C = 0x04;
    port[8].C = 0x08;
    port[9].C = 0x10;

    arraySize = sizeof(port) / sizeof(port[0]);

    for (blockValue = 0; blockValue < arraySize; blockValue++)
    {
        for (i = 0; i <= blockValue; i++)
        {
            PORTA = port[i].A;
            PORTB = port[i].B;
            PORTC = port[i].C;
        }
        
        delayTMR(100);
    }


 
 
 

post edited by InvalidApple - 2019/05/22 06:05:38
#10
1and0
Access is Denied
  • Total Posts : 9200
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 06:44:20 (permalink)
+1 (1)
parkcoding
 
PORTA = arr[0];
PORTB = arr[1];
PORTB = arr[2];
PORTB = arr[3];
PORTB = arr[4];
PORTC = arr[5];
PORTC = arr[6];
PORTC = arr[7];
PORTC = arr[8];
PORTC = arr[9];
delayTMR(100);

I don't think persistence of vision will work here; i.e. that sequence will probably be seen as

PORTA = arr[0];
PORTB = arr[4];
PORTC = arr[9];
delayTMR(100);

#11
NorthGuy
Super Member
  • Total Posts : 5428
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 07:48:38 (permalink)
+1 (1)
Not to mention that PORTA, once set at the beginning, never changes.
#12
pcbbc
Super Member
  • Total Posts : 992
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: How should I write repetitive sentences 2019/05/22 09:09:42 (permalink)
+1 (1)
NorthGuy
Not to mention that PORTA, once set at the beginning, never changes.



Yep - See #9.
 
All this confusion because...
parkcoding
What I'm trying to do is turn on sequential the light.

...doesn't come close to a specification.
#13
parkcoding
Starting Member
  • Total Posts : 40
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 16:33:25 (permalink)
0
Thank you!!!
There are so many different ways.
It was very helpful.
Have a wonderful day today.
#14
parkcoding
Starting Member
  • Total Posts : 40
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 16:59:45 (permalink)
0
How can I turn off led in array when I have to turn it off according to repetitive sequential?
I don't know well because I'm a beginner. Thank you for your help.
 
 
unsigned char brr[10] = {0x08, 0x10, 0x02, 0x04, 0x80, 0x01, 0x20, 0x40, 0x04, 0x10};
 
PORTC = brr[0];
delayTMR(100);

PORTC = brr[0];
PORTC = brr[1];
delayTMR(100);

PORTC = brr[0];
PORTC = brr[1];
PORTC = brr[2];
delayTMR(100);

PORTC = brr[0];
PORTC = brr[1];
PORTC = brr[2];
PORTC = brr[3];
delayTMR(100);

PORTC = brr[0];
PORTC = brr[1];
PORTC = brr[2];
PORTC = brr[3];
PORTC = brr[4];
delayTMR(100);

PORTC = brr[0];
PORTC = brr[1];
PORTC = brr[2];
PORTC = brr[3];
PORTC = brr[4];
PORTB = brr[5];
delayTMR(100);

PORTC = brr[0];
PORTC = brr[1];
PORTC = brr[2];
PORTC = brr[3];
PORTC = brr[4];
PORTB = brr[5];
PORTB = brr[6];
delayTMR(100);

PORTC = brr[0];
PORTC = brr[1];
PORTC = brr[2];
PORTC = brr[3];
PORTC = brr[4];
PORTB = brr[5];
PORTB = brr[6];
PORTB = brr[7];
delayTMR(100);

PORTC = brr[0];
PORTC = brr[1];
PORTC = brr[2];
PORTC = brr[3];
PORTC = brr[4];
PORTB = brr[5];
PORTB = brr[6];
PORTB = brr[7];
PORTB = brr[8];
delayTMR(100);

PORTC = brr[0];
PORTC = brr[1];
PORTC = brr[2];
PORTC = brr[3];
PORTC = brr[4];
PORTB = brr[5];
PORTB = brr[6];
PORTB = brr[7];
PORTB = brr[8];
PORTA = brr[9];
delayTMR(100);
#15
InvalidApple
Super Member
  • Total Posts : 286
  • Reward points : 0
  • Joined: 2011/05/17 23:36:35
  • Location: Melbourne, Australia
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 18:05:23 (permalink)
+1 (1)
So a different pattern, but going backwards?
#16
parkcoding
Starting Member
  • Total Posts : 40
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 18:31:50 (permalink)
0
yes, I want sequential led on/off
arr[] is sequential led on array
and brr[] is sequential led off array.
post edited by parkcoding - 2019/05/22 18:34:23
#17
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 19:41:44 (permalink)
+1 (1)
parkcoding
yes, I want sequential led on/off
arr[] is sequential led on array
and brr[] is sequential led off array.


Do you realise that when you write two values in succession to PORTC, you're not going to see the first value at all unless you add a delay between each write?
 

Nearly there...
#18
Aussie Susan
Super Member
  • Total Posts : 3559
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 20:00:09 (permalink)
+1 (1)
If a LED is connected to the PORT pin (in series with a resistor of course) to (say) ground, then taking the PORT pin high (1) will turn the LED on and taking it low (0) will turn the LED off.
You would appear to have a series of LEDs connected to the pins of each PORT. Therefore when you write (say) 0x0f to a port, the LEDs on pins 0, 1, 2 and 3 will go in (as you have written a '1' to those ports pins, and the LEDs on pins 4, 5, 6 and 7 will go off (you have written a 0 to those pins).
The next time you write to the same PORT, you will over-write whatever was there before and the new pattern will be displayed. For example, if after writing 0x0f, you then write 0x1e, the LED on pin 0 will go off and the LED on pin 4 will go on.
The reason you need the delays between the writes is that the device can execute instructions at a very fast rate. For example, it is quite possible for you to update the PORT pins every few uSec. As your eyes can only react to changes in the order of 40 mSec or slower (that would be 25 frames a second for a movie which appears to most people to not flicker) you would not see any changes in the LEDs without putting in (say) 100mSec delays or longer between the PORT writes. (This is why some of the suggestions above only use the last values you write to a PORT - the other values will never be noticed by you.)
Susan
#19
InvalidApple
Super Member
  • Total Posts : 286
  • Reward points : 0
  • Joined: 2011/05/17 23:36:35
  • Location: Melbourne, Australia
  • Status: offline
Re: How should I write repetitive sentences 2019/05/22 21:34:02 (permalink) ☄ Helpfulby parkcoding 2019/05/22 22:35:56
+1 (1)
parkcoding
yes, I want sequential led on/offarr[] is sequential led on arrayand brr[] is sequential led off array.


Use the same "for" loop, but just change the inner "if" statement.

It is a really good project to get you started :)

It is a good idea to go through a few tutorials for the C language -
https://www.cprogramming...tutorial/c/lesson1.html
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5