Pages: [1] 2   Go Down
Author Topic: how would i code this  (Read 933 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Online Online
Faraday Member
**
Karma: 82
Posts: 3832
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26420
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe something like this
Code:
// 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);}
}
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Maybe something like this
But properly indented...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26420
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 07, 2013, 10:10:27 pm by CrossRoads » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 46
Posts: 2865
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Bob
Logged

--> 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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26420
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Indented sample code:
Code:
// 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);
  }
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 46
Posts: 2865
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Personally I like my curly's Right to the Wall. The Left wall that is...
Logged

--> 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

Online Online
Faraday Member
**
Karma: 82
Posts: 3832
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.....
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 792
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2   Go Up
Jump to: