Go Down

Topic: How to stop when reached maximum? (Read 253 times) previous topic - next topic

JiYeon

Hi everyone, This is the first time for me to use an arduino. I am doing a small project on making a some sort of data logger, which will be measuring voltage and current with varies load resistance and calculate the power. I have been wondering around the internet, but it seems that the resources can't resolve my problem.

Code: [Select]

    while(inByte=='s'){
      analogWrite(lampPin, 191);
      Measurement();
      digitalWrite(rledPin, LOW);
      digitalWrite(gledPin, HIGH);
    }
  }
}

void Measurement() {
  int VoltageSensor = analogRead(A0);
  int CurrentSensor = analogRead(A1);
  float voltage = VoltageSensor* (5.0 / 1023.0);
  float current = CurrentSensor* (5.0 / 153603.5);
  float power = voltage* current;
  Serial.print(voltage, 5);
  Serial.print('\t');
  Serial.print(current, 5);
  Serial.print('\t');
  Serial.print(power, 5);
  Serial.println();
  delay(1000);
}


How do I tell arduino stop when the maximum power is reached?
My idea is like :
1) if nowPower>prevPower, keep reading
2) if prevPower>nowPower, stop reading, prevPower=maxPower
3) Serial.print(maxPower)

Not sure how to do it exactly, and one last thing, in my sketch when I press 's' in the monitor, it will start measuring, the problem is I don't know how to break the while loop, I have tried to use delay and break, they didn't seem to work, maybe I have done something wrong.

Please help

UKHeliBob

The first thing to do is to post your whole program, not just the bit(s) that you think are important
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

afremont

Quote
My idea is like :
1) if nowPower>prevPower, keep reading
2) if prevPower>nowPower, stop reading, prevPower=maxPower
3) Serial.print(maxPower)


Step 1 should be:
1) if nowPower>prevPower, set prevPower = nowPower, keep reading

Step 2 should be more like:
2) if prevPower>nowPower, maxPower=prevPower, stop reading

I don't know why break wouldn't exit the loop, It should have; I suspect it did, but you have another infinite loop surrounding it.  Do you really want a while loop?  Another way to exit the loop that is more conventional is to make sure the condition (inByte == 's') becomes non-true at some point.  I would have expected inByte to change at some point causing the loop to terminate normally.
Experience, it's what you get when you were expecting something else.

Go Up