-3

Can you help me writing a code for this question guys?? You have an Arduino Uno with 8 LEDs connected to digital pins 2 to 9. Create an array of 8 elements. Fill it with random numbers between 2 and 9 (all inclusive) Write a program which determines the greatest number among them. Save this number in a variable named max. Print this number on serial monitor. Blink the LED connected to this number. Let the delay time be 0.5 second.

Note that the random function may result in duplicate numbers generated. For example, the generated array may be {3,4, 4, 7,6, 2,5,7}

The maximum number will be 7.Since it occurred twice in the array, then you should blink it twice ^_^ It the maximum number occured 3 times in the array, then you should blink it 3 times, and so for any number of occurrences

I have written this so far but I'm sure it's all wrong

int pins[8]={2, 3, 4, 5, 6, 7, 8,9};

void setup() 
{
   pinMode(pins[8], OUTPUT);
}

void loop() 
{
    x=random(0, 8);
    digitalWrite(pins[x], HIGH);
    delay(1000)
    digitalWrite(pins[x], LOW);
    delay(1000)
}
  • 1
    That's poorly stated homework since they only give one delay but expect patterned blinking. Anyway, approach this by imagining you are a robot being shown the numbers one at a time. What very efficient notes could you take on a re-writable scratchpad (ie, a minimal number of variables) It will help in finding the best solution if you assume the list of numbers could be arbitrarily long... – Chris Stratton Mar 11 '16 at 13:47
  • @JRobert I'm stuck in the if statement and arrays I really can't understand them also there's supposed to be a for loop – user19167 Mar 11 '16 at 14:54
  • How would you simulate this on paper/physically? For example, using N head-tail coins as LEDs..... – Dave X Mar 11 '16 at 16:54
  • Well for one improvement, pins[8] is undefined, since C indices start with 0, so there are only pins[0]...pins[7] defined in your code. Maybe you could use your for(;;) loop there? Also, try compiling your code--if the compiler detects and reports errors, it might help you refine your logic. – Dave X Mar 11 '16 at 16:59
  • 1
    Frankly there is no sound reason to put the pins in an array at all, since their range is continuous. The only array needed is the input data. And only two local variables plus the iterator variable are required to solve the problem. Basically, writing code is premature. Figure out the algorithm. – Chris Stratton Mar 11 '16 at 18:20
2

First hint: pinMode() only applies to one pin at a time. You'll need to execute it 8 times to intialize 8 I/O pins' modes.

Second hint: As @ChrisStratton tried to suggest, generate the random number set first; then "figure out" (set some variables in memory that will tell you) what do with the terminal output and the digital I/Os; then do each of those things. It helps to partition a larger problem into smaller ones.

Update: Considering your updated title, How do I learn C?, and since the question reads like a homework assignment, aren't you already taking a class in C or C++? If so, how do you find it lacking, i.e., what additional resources or direction would be most helpful to you? There are a number of good books on the C language, and one of those would be a good place to start. If you don't already have a favorite one, you can't go wrong with The C Programming Language, by Brian Kernighan & Dennis Ritchie, the authors/designers of the C language and its early compilers. It is book on the C language, not on programming per-se, so if you are new to programming as well, a programming tutorial would be an excellent additional choice (sorry I don't have one to recommend).

Secondly, beat up on the example programs that come with the ArduinoIDE: click the "Open" button [uparrow icon on the IDE toolbar; not the File | Open menu] and you'll see a menu of your current sketches and, below the break, categories of example programs. Pick one of the examples, read it and try to understand what it does and why it is written the way it is. Then tweak the ###p out of it - both to understand how make it behave differently, and to break it. You'll learn a lot by a) interpreting error messages from the compiler, and b) finding and fixing your mistakes. If you're not breaking your programs at least twice as often as you're succeeding, you're not trying hard enough!

Not the answer you're looking for? Browse other questions tagged or ask your own question.