Go Down

Topic: Need help understanding a line in someone elses sketch (Read 545 times) previous topic - next topic

Bill2k

Can someone explain what this line will return. It is located in the first if statement located in the first loop.

objectPresent = (powerCount < powerCountThreshold);

Code: [Select]
// Project 13 - Anti-gravity
// 15 Dangerous Projects for the Evil Genius

#define coilPin 11
#define irPin 13
#define sensorPin 0

int A = 2;
// Adjust B to improve stability
int B = 60;
int C = 20;
int D = 1000;

int maxPower = 255;
long powerCountThreshold = 300000;
int objectPresent = 0;
int monitoring = false;

void setup()
{
  pinMode(coilPin, OUTPUT);
  pinMode(irPin, OUTPUT);
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
  Serial.println("Ready");
  Serial.println("m - toggle monitoring");
  Serial.println("B - increase B");
  Serial.println("b - decrease B");
}

void loop()
{
  static int count = 0;
  static int oldPosition = 0;
  static int ambient = 0;
  static long powerCount = 0;
  count ++;
 
  if (count == 1000)
  {
    ambient = readAmbient();
    count = 0;
    objectPresent = (powerCount < powerCountThreshold);
    powerCount = 0;
  }
  int raw = 1024 - analogRead(sensorPin);
  // position from top (0) of sensor region to the bottom (650)
  int position = raw - ambient;
  // positive value means going downwards, negative going upwards
  int velocity = position - oldPosition;
  int power = position / A + velocity * B + C;

  powerCount += power;
  oldPosition = position;
 
  // clip
  if (power > maxPower) power = maxPower;
  if (power < 0) power = 0;

  checkSerial();
 
  if (monitoring)
  {
    Serial.print(position);  Serial.print(",");
    Serial.println(velocity);
  }
 
  analogWrite(coilPin, power * objectPresent);
  delayMicroseconds(D);
}

int readAmbient()   //todo try speding up delay in micros
{
  digitalWrite(irPin, LOW);
   // allow time for LED and phototransistor to settle
  delayMicroseconds(100);
  int ambient = 1024 - analogRead(sensorPin);
  digitalWrite(irPin, HIGH);
  return ambient;
}

void checkSerial()
{
  if (Serial.available())
  {
    char ch = Serial.read();
    if (ch == 'm')
    {
      monitoring = ! monitoring;
    }
    if (ch == 'B')
    {
      B += 5;
      Serial.println(B);
    }
    if (ch == 'b')
    {
      B -= 5;
      Serial.println(B);
    }
  }
}


Thanks


Delta_G

If powerCount is less than powerCountThreshold then the part in the parenthesis will be true so objectPresent gets set to true.

Otherwise it will be set to false in the same way.

Chaul

#2
Feb 08, 2013, 07:54 pm Last Edit: Feb 08, 2013, 07:55 pm by Chaul Reason: 1
That's a little awkward because objectPresent is defined as an int and (powerCount < powerCountThreshold) equals to either true or false. I guess it works because true can equal to 1 and false to 0 in the end. Then it's used in an equation power * objectPresent and for that you need a number int anyways.

Edit: In other environments, like Windows COM services, that wouldn't be a safe assumption at all...

Bill2k

#3
Feb 08, 2013, 08:01 pm Last Edit: Feb 08, 2013, 08:07 pm by Bill2k Reason: 1
Ok, thats what I thought but I wasn't sure. The only other place objectPresent is used is in the analogWrite statement.

One more quick question,

Right after the first if statement, "int position" is declared. Why is it declared in that part of the code and not with the rest of the declarations? Also, why is it highlighted?

Thanks again

PaulS

Quote
Why is it declared in that part of the code and not with the rest of the declarations?

Because it is local to that function. The real question is why there are so many global variables when most of them should be local or passed as arguments.

Quote
Also, why is it highlighted?

It's not, on the forum. You need to explain what this means.

Bill2k



Quote
Also, why is it highlighted?

It's not, on the forum. You need to explain what this means.


When loaded int the Arduino IDE, "int position" is highlighted orange like a Serial.print would be highlighted.

Arrch




Quote
Also, why is it highlighted?

It's not, on the forum. You need to explain what this means.


When loaded int the Arduino IDE, "int position" is highlighted orange like a Serial.print would be highlighted.


You have a library installed that has declared "position" to be a keyword, and thus, "highlights" it.

Bill2k

Hrmm, I wonder which library that is. I didn't import any libraries into this sketch. Is it one of the IDE's basic libraries?

Arrch


Hrmm, I wonder which library that is. I didn't import any libraries into this sketch. Is it one of the IDE's basic libraries?



Doesn't matter if it was imported into the sketch or not, the library just has to be installed. It's purely cosmetic, though, it doesn't affect functionality.

Bill2k


Go Up