Go Down

Topic: how would i code this (Read 1 time) previous topic - next topic

jareeb

i want to have two or three inputs (pot,thermistor,switch)
the pot and the thermistor need to tell how long it needs to say on and the switch just tells it when to turn on.

so like if pot is 125 and thermistor is at 336 than the output needs to stay on for 12ms or something like that, but no switch on till the switch reads high.

JimboZA

First thing I'd do is establish the relationship between the pot value and the thermistor value value on the one hand, and the on-time on the other, ie on-time = f(pot-value, thermistor-value) (eg, like you say, if they are 125 and 336, on-time is 12)

Then code something like this very-pseudo-code:

Code: [Select]
read the pot
read the thermistor
on-time = ? // whatever the relationship is

read the switch
if switch is high, set output-pin high and delay for on-time

"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

CrossRoads

Maybe something like this
Code: [Select]

// declare variables, pinModes, etc.

void loop(){
delay(2);
timeOne = analogRead(A0);
delay(2);
timeTwo = analogRead(A1);
onTime = abs(timeOne - timeTwo);
if (digitalRead(switchPin) == HIGH && timeRunning == 0){
timeRunning = 1;
startTime = millis();
digitalWrite(ledPin,  HIGH);
endTime = startTime + onTime;
}
if ( timeRunning == 1 && (millis() >= endTime) ){
timeRunning = 0;
digitalWrite(ledPin, LOW);}
}
}
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

PaulS

Quote
Maybe something like this

But properly indented...

jareeb

I was thinking maybe using a 2d array but im not sure how to use them. Have the range of a pot on one side and the range of the thermistor on the other side and the run time in ms in the table?
im not at home so i can't test any of those sketches.

PeterH


I was thinking maybe using a 2d array but im not sure how to use them.


Do you really need to resort to a look-up table - can't you come up with an algorithm to calculate the time from the two input values?
I only provide help via the forum - please do not contact me for private consultancy.

CrossRoads

#6
Feb 08, 2013, 03:56 am Last Edit: Feb 08, 2013, 04:10 am by CrossRoads Reason: 1
Quote
But properly indented...

Yeah, I don't do indenting when I'm making it up on the fly and not home with the IDE.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Docedison

You Mr Crossroads, set a poor example... OTOH If he wrote code like I write letters.. No one could read it..

Bob
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

This is one probable answer



void setup(){
Serial.begin(9600);
pinMode (pin, INPUT);
}

void loop{
int pot = analogread(//pin);
int thermoresistor = analogread(//pin);

if (digitalRead(pin)==HIGH){
 //your command
}


}//end void loop

CrossRoads

Indented sample code:
Code: [Select]

// declare variables, pinModes, etc.

void loop(){
  delay(2);
  timeOne = analogRead(A0);
  delay(2);
  timeTwo = analogRead(A1);
  onTime = abs(timeOne - timeTwo);
  if (digitalRead(switchPin) == HIGH && timeRunning == 0){
    timeRunning = 1;
    startTime = millis();
    digitalWrite(ledPin,  HIGH);
    endTime = startTime + onTime;
  }
  if ( timeRunning == 1 && (millis() >= endTime) ){
    timeRunning = 0;
    digitalWrite(ledPin, LOW);
  }
}
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Docedison

Personally I like my curly's Right to the Wall. The Left wall that is...
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

JimboZA

Quote
can't you come up with an algorithm to calculate the time from the two input values?


That's what I was leaning towards with my on-time = f(pot-value, thermistor-value)

This might be a good candidate for a fuzzy approach.....
"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

jareeb

Im not sure i think a look up table would ve better as it would help speed up reprograming. Just a 8 by 8 table would work fine. Any good tuts on that?

Krupski


i want to have two or three inputs (pot,thermistor,switch)
the pot and the thermistor need to tell how long it needs to say on and the switch just tells it when to turn on.

so like if pot is 125 and thermistor is at 336 than the output needs to stay on for 12ms or something like that, but no switch on till the switch reads high.


It sounds like you are trying to make a closed loop temperature controller. If so, then using predefined values to control the "switch" will not work. It will be virtually impossible to get the right constants. That method would be "open loop".

What you need to do is read your thermistor and turn the "switch" on for a time interval proportional to the DIFFERENCE between your "setpoint" (pot setting) and the actual reading (thermistor).

The on time will get smaller and smaller until the setpoint it reached, then the circuit will just slowly kick the switch on and off to maintain the setpoint.
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

jareeb

Its not to keep a constant temperature. Its to contol an injector. It needs to be adjustable to temperature change and how much the throttle body is opened and only open when it needs to.

Go Up