Need a little assistance

I’m not going to give out my sob story, so to keep things short, I’m working on an autonomous sumo robot for my first ever arduino project. Right now, I’m having an issue getting an analog value from from an IR sensor(sensorPin) in order to change the state of the motors. I’m pretty sure the problem has to do with my global variable definitions. There are many more changes to come to complete the code for this robot but this roadblock had me stuck for a few hours.

Disclaimer: Keep in mind that the code you are about to view may be hard to view. This may be hard for some viewers to look at. If you are at all squeamish, I ask that you please look away now.

const int aIp0 = A0;
const int aIp1 = A1;
const int aIp2 = A3;
const int aIp3 = A3;
const int aIp4 = A4;
int sensorPin = 0; 
int QRE1113_PinFL= 0;
int QRE1113_PinFR = 0;
int QRE1113_PinBL = 0;
int QRE1113_PinBR = 0;
int pwm_a = 3 ;
int pwm_b = 11;
int dir_a = 12;
int dir_b = 13;
int QRE_ValueFL = 0; 
int QRE_ValueFR = 0;
int QRE_ValueBL = 0;
int QRE_ValueBR = 0;
int FrontSensor = 0;


void setup(){
  Serial.begin(9600);
  
  pinMode (pwm_a, OUTPUT);
  pinMode (pwm_b, OUTPUT);
  pinMode (dir_a, OUTPUT);
  pinMode (dir_b, OUTPUT);
}

void update_sensors(){
  
  sensorPin = analogRead(aIp0);
  FrontSensor = map(sensorPin, 0, 1023, 0, 255);
  
  Serial.println(FrontSensor);
  
  int QRE_ValueFL = analogRead(aIp1);
  
  int QRE_ValueFR = analogRead(aIp2);
  
  int QRE_ValueBL = analogRead(aIp3);
  
  int QRE_ValueBR = analogRead(aIp4);
  
  delay(1000);
}

void loop(){
  if (FrontSensor < 20)
  {
          analogWrite (pwm_a, 150);
          analogWrite (pwm_b, 0);
          delay (1000);
          analogWrite (pwm_a, 0);
          analogWrite (pwm_b, 150);
          delay (1000);
  } 
   else if(FrontSensor > 20);
  {
    analogWrite (pwm_a, 150);
    analogWrite (pwm_b, 150);
    
  }
}

I can explain my reasoning for whatever questions come up but bear in mind, this is my first project with arduino. I know it looks nasty

Can you explain why you haven't got any debug prints to help you figure out what may be wrong?

opps

There's no point using a map statement, just /4.

What are these lines for?

int QRE_ValueFL = analogRead(aIp1);

int QRE_ValueFR = analogRead(aIp2);

int QRE_ValueBL = analogRead(aIp3);

int QRE_ValueBR = analogRead(aIp4);


Rob

int sensorPin = 0; 
  sensorPin = analogRead(aIp0);

Some of your names need some work. Pin in the name generally denotes a pin number, not the Val(ue) read from the pin.

You are updating the "FrontSensor" in a function called "update_sensors()", but you never call that function.

AWOL:
Can you explain why you haven’t got any debug prints to help you figure out what may be wrong?

Yes I can. I have no clue about the code or its functions. Not only is this my first project with arduino but I have had next to no programming experience until last night.

Graynomad:
There’s no point using a map statement, just /4.

What are these lines for?

int QRE_ValueFL = analogRead(aIp1);

int QRE_ValueFR = analogRead(aIp2);

int QRE_ValueBL = analogRead(aIp3);

int QRE_ValueBR = analogRead(aIp4);


Rob

They’re other analog inputs from other sensors I need to work with a little later. I was trying to clear the initial sensor error before I jump into those. I could comment them out for now but not I’m not sure if it matters either way.

PaulS:

int sensorPin = 0; 

sensorPin = analogRead(aIp0);



Some of your names need some work. Pin in the name generally denotes a pin number, not the Val(ue) read from the pin.

They do indeed need work. I plan on simplifying everything as soon as I get some results.

This is a modified/simplified version of what I’m doing now

int analogPin0;
int fs = 0;

void update_sensors(){
fs = analogRead(analogPin0);
Serial.println(fs); 
}
int analogPin0;
int fs = 0;

void update_sensors(){
fs = analogRead(analogPin0);
Serial.println(fs); 
}

When naming pins with variables, you should be using names that define their purpose. The reason to use a variable in the first place is so that if you need to change the pin, you only have to change a single line of code. That's still the case right now, but it would be a bit odd to have a variable named analogPin0 with a value of 5.

[quote author=James C4S link=topic=107321.msg805862#msg805862 date=1337956818] You are updating the "FrontSensor" in a function called "update_sensors()", but you never call that function. [/quote]

Just trying to think back on the programming class I took, at the end of the "update" function, I return the value and pass it threw the "loop" structure?....

TBH, Im not sure this reply even makes sense to me. The programming class I took was long ago and we spent a day on actually calling functions.

sightfulrage: TBH, Im not sure this reply even makes sense to me. The programming class I took was long ago and we spent a day on actually calling functions.

You created a function called "update_sensors()". Unless you call that function, the code never runs. So inside of loop() you need to actually call update_sensors().

E.g.

void loop() {
   update_sensors();
   // the rest of your code

Arrch: ``` int analogPin0; int fs = 0;

void update_sensors(){ fs = analogRead(analogPin0); Serial.println(fs); }




When naming pins with variables, you should be using names that define their purpose. The reason to use a variable in the first place is so that if you need to change the pin, you only have to change a single line of code. That's still the case right now, but it would be a bit odd to have a variable named analogPin0 with a value of 5.

What I thought I was doing with that basic code was:

Defining the analogPin0 as analog input 0 Reading from analogPin0 Placing the value into fs Printing the value of fs.

This was not right. I doubt that my initial definition of analogPin0 close.

sightfulrage: What I thought I was doing with that basic code was:

Defining the analogPin0 as analog input 0 Reading from analogPin0 Placing the value into fs Printing the value of fs.

This was not right. I doubt that my initial definition of analogPin0 close.

That wasn't my point. Defining a variable as "analogPin0" tells us where it plugs in, but not what it's purpose is. It's better form to call the variable something like "lightSensorPin" or "tempSensorPin" as that tells us it's purpose and allows us to change pins with 1 line of code, if needed, without making it confusing.

[quote author=James C4S link=topic=107321.msg805894#msg805894 date=1337958704]

sightfulrage: TBH, Im not sure this reply even makes sense to me. The programming class I took was long ago and we spent a day on actually calling functions.

You created a function called "update_sensors()". Unless you call that function, the code never runs. So inside of loop() you need to actually call update_sensors().

E.g.

void loop() {
   update_sensors();
   // the rest of your code

[/quote]

Wow.... Yeah. All I can say is it finally does what I need it to do! So happy I could kiss you right now. But since your in Texas, I'll leave it alone

sightfulrage:

Graynomad: What are these lines for?

int QRE_ValueFL = analogRead(aIp1);

int QRE_ValueFR = analogRead(aIp2);

int QRE_ValueBL = analogRead(aIp3);

int QRE_ValueBR = analogRead(aIp4);

They're other analog inputs from other sensors I need to work with a little later. I was trying to clear the initial sensor error before I jump into those. I could comment them out for now but not I'm not sure if it matters either way.

When you come to using those extra inputs you need to delete the words 'int' from here. These are declaring a new local variable which you're storing the sensor values in, whereas I'm sure you intended to store the values in the global variables with the same name.