Jump to content

Anybody code an with an arduino?

asiancorruption

Does anybody code with an arduino? I can't figure out the problems with my code and I would like someone to look over my code to see if it would to what I intend for it do to. 

 

What it's supposed to do: generate a number and use 8 leds on a breadboard to signify if the guess is too high, low or spot on, and respond accordingly. 

 

What's wrong with my code: "serial  doesn't have a type", also, idk how my leds are going to react. are they gonna all blink at once with a 100ms delay, or are they gonna blink once at a time in order?

If you can help me, that'd be great. 

 

 

 

 

 

char answer;
int Number;
int randNumber;
void setup();
Serial.begin(9600) {
  pinMode(2, OUTPUT), pinMode(3, OUTPUT), pinMode(4, OUTPUT) , pinMode(5, OUTPUT) , pinMode(6, OUTPUT) , pinMode(7, OUTPUT) , pinMode(8, OUTPUT) , pinMode(9, OUTPUT);
}

 

void loop() {
  Serial.println("Guess a number between 0-255");
  number = Serial.Readline;
           randNumber = random(255);
  while (Serial.available() == 0);
  number = Serial.parseInt();

  while (number < 255({
  //if (money)<255// the computer will generate a random number
  Serial.println("guess the number")
  };
  while (Serial.available() == 0);
  answer = Serial.read();

  if (guess > Num) {
    (
      answer == 'n');
      printIn("Guess is too low, try again.");
      digitalWrite(2, 3, 4, 5, 6, 7, 8, 9)LOW);
      delay(100);
    }

  if (guess < Num) {
  answer == 'n')
  printIn("Guess is too high, try again.");
  digitalWrite(9, 8, 7, 6, 5, 4, 3, 2, 1) HIGH);
  delay(100);

}
else {
  yes
  number = number = Number
                    println ("you won! *cue John Cena theme song*");
    digitalWrite(2, 3, 4, 5, 6, 7, 8, 9 HIGH);

  }
}

Link to comment
Share on other sites

Link to post
Share on other sites

To me it looks like they blink all at once. I've done very little arduino programming so I can't tell you how to solve your problem. Sorry :(

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not gonna give you any code verbatim, because that's pointless and you won't learn. What I will do is highlight things that you appear to not grasp and suggest how to actually go about it. If I sound rude at all, I apologize, that's not my intent. Now on to the problem.

 

From the little Arduino work I've done (I mainly use Xylinx/ Quartus/ Keil/ CodeWarrior/ Atmel Studio), I know that an arduino "sketch" needs a minimum of two functions: a setup function and a loop function. It appears that you have declared the setup function, but it does nothing. Try fixing the scope operators, {}, so that what you want setup() to do is contained within it's scope.

 

When you call pinMode(), since it is a function/ method call, you need to indicate the end of the call with a ;. You need to do this each time you call the function, or any other, for that matter.

 

Readline does not exist, at least according to a quick google search. Try using something else. Also, you would use it as a function.

 

while (number < 255({
  //if (money)<255// the computer will generate a random number
  Serial.println("guess the number")
  };

 

^Not at all sure what this is for. However, for while loops, try to follow this structure:

while(condition)

{

      // Do stuff

      myFunc();

}

 

This is also a correct use of a while loop, if you want to wait until some condition has been met. I've seen it used a few times in your code, and what I saw seemed fine.

while(condition);

 

I don't believe you are using digitalWrite() correctly. Check the input parameters online and try to match that. You may have to make multiple function calls instead of just one. You also have random () in the code. 

 

Variable "guess" is used but never declared/ instantiated. Be consistent. I believe you meant to put "answer" here, but I could be wrong.

 

The way your digitalWrite is written, it looks like you intend to blink the LEDs all at once. However, in order to blink the LEDs, you first have to turn them on (pins HIGH or LOW depends on your circuit), wait for some delay, then turn them off. So if you want them to blink in succession, you will have to write this for each LED seperately, instead of turning them all on, calling delay once, then turning them all of. 

CPU - FX 8320 @ 4.8 GHz

| MoBo - Sabertooth 990FX | GPU - XFX Radeon HD 7870 GHz Ed. @ 1.075 GHz | CPU Cooler - H100 | RAM - 16 GB Dual Channel Vengeance @ 1600 MHz (didn't care to push these...) | OS - Windows 8 Pro | Storage - OCZ Vertex 3 (120 GB Boot), Samsung 830 Pro 64 GB, WD Black 1 TB, some random 320 GB from a laptop | PSU - CM Silent Hybrid Pro 850W (MDPC Sleeving) | Case - 800D | Monitors - ASUS V238H/ X Star DP2710LED | Mouse - M90 Keyboard - CM Quickfire Rapid w/ custom key caps

"When life gives you lemons, Don't make lemonade, make life take the lemons back!" - Cave Johnson, CEO

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×