Go Down

Topic: Trying to make 2 sensors work but this happens  (Read 356 times) previous topic - next topic

NewZealandKiwiKid

Hi! Wonder if anyone could help me out here with this code because I keep getting several problems. Whenever I try to upload my code, I get an error message saying 'exit status 1 expected unqualified-id before 'if''

Here's my code:

int val = 0;

void setup()
{
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  digitalWrite(9,HIGH);
  digitalWrite(8,LOW);
  digitalWrite(13,HIGH);
  digitalWrite(12,LOW);
}
if(analogRead(A0) > 500) // This is the Left Sensor
{
  digitalWrite(8,LOW); // Stops the Right motor from moving
  digitalWrite(9,LOW);
}
else
{
  digitalWrite(9,HIGH); // Makes the Right motor move foward
  digitalWrite(8,LOW);
  end
}
if(analogRead(A3) > 500) // This is the Right Sensor
{
  digitalWrite(13,LOW); // Stops the Left Motor from moving
  digitalWrite(12,LOW);
}
else
{
  digitalWrite(13,HIGH); // This makes the Left motor move foward.
  digitalWrite(12,LOW);
}
Serial.println(val); // Prints the Sensor Value
  }


If you can solve it. Thanks!
Coding is what I want to learn, especially LUA Coding. But building inside games is what I know.

DrAzzy

You have the if statements outside of the loop function; you cannot have code outside of a function.

It looks like you have an extra } after the block of digitalWrite()'s
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

NewZealandKiwiKid

Mind showing me an example what you mean by that?
Coding is what I want to learn, especially LUA Coding. But building inside games is what I know.

DrAzzy

Code: [Select]
int val = 0;

void setup()
{
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  digitalWrite(9,HIGH);
  digitalWrite(8,LOW);
  digitalWrite(13,HIGH);
  digitalWrite(12,LOW);
} //DELETE THIS - this closes the loop() function, so all the code after it is not in a function, which is not valid
if(analogRead(A0) > 500) // This is the Left Sensor
{
  digitalWrite(8,LOW); // Stops the Right motor from moving
  digitalWrite(9,LOW);
}
else
{
  digitalWrite(9,HIGH); // Makes the Right motor move foward
  digitalWrite(8,LOW);
  end
}
if(analogRead(A3) > 500) // This is the Right Sensor
{
  digitalWrite(13,LOW); // Stops the Left Motor from moving
  digitalWrite(12,LOW);
}
else
{
  digitalWrite(13,HIGH); // This makes the Left motor move foward.
  digitalWrite(12,LOW);
}
Serial.println(val); // Prints the Sensor Value
  }
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

NewZealandKiwiKid

Thanks for that it worked but the second part of the analogRead is still having troubles. Im doing the same way you told me to do it but now the '}' is having problems at the end, I delete the last last one. I delete it and it says it has problems. I put it back. It says "expected '}' at end of input" and if I delete it this is what it says the same thing.  :(
Coding is what I want to learn, especially LUA Coding. But building inside games is what I know.

jremington

#5
May 27, 2019, 11:03 pm Last Edit: May 27, 2019, 11:03 pm by jremington
Always post the revised code, using code tags.

NewZealandKiwiKid

Ok so a friend helped me out and the code works now! :D

But there is one problem. The wheels wont move and if I try printing the serial so I can see, it's stuck at the number '243' and it wont go up or down.

But the main problem is the sensors work but the wheels don't move one pixel.
Coding is what I want to learn, especially LUA Coding. But building inside games is what I know.

jremington


NewZealandKiwiKid

Coding is what I want to learn, especially LUA Coding. But building inside games is what I know.

jremington


NewZealandKiwiKid

Coding is what I want to learn, especially LUA Coding. But building inside games is what I know.

NewZealandKiwiKid

So my code works and all it's just that the wheels wont move at all. I don't know what to do about it.
Coding is what I want to learn, especially LUA Coding. But building inside games is what I know.

jremington

#12
Jun 05, 2019, 03:12 am Last Edit: Jun 05, 2019, 03:34 am by jremington
As before, follow the directions in the "How to use this forum" post and post your code, wiring diagram and links to the parts.

Thusly informed forum members might be able to help.

Go Up