Loading...
Pages: [1] 2   Go Down
Author Topic: how would i code this  (Read 373 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

Johannesburg UTC+2
Offline Offline
Edison Member
*
Karma: 34
Posts: 1705
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

IT Crowd:
Roy... "Have you tried turning it off and on again?"
Moss.. "Have you tried forcing an unexpected reboot?"

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 249
Posts: 16572
Available for Design & Build services
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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 316
Posts: 35590
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
Tesla Member
***
Karma: 89
Posts: 6406
-
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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 249
Posts: 16572
Available for Design & Build services
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

Anaheim CA.
Offline Offline
Edison Member
*
Karma: 31
Posts: 2311
Experienced old Whitebeard with a Full head of Hair...
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

“The solution of every problem is another problem.” -Johann Wolfgang von Goethe

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: 249
Posts: 16572
Available for Design & Build services
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

Anaheim CA.
Offline Offline
Edison Member
*
Karma: 31
Posts: 2311
Experienced old Whitebeard with a Full head of Hair...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

“The solution of every problem is another problem.” -Johann Wolfgang von Goethe

Johannesburg UTC+2
Offline Offline
Edison Member
*
Karma: 34
Posts: 1705
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

IT Crowd:
Roy... "Have you tried turning it off and on again?"
Moss.. "Have you tried forcing an unexpected reboot?"

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

Offline Offline
Sr. Member
****
Karma: 15
Posts: 463
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.

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

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
Print
 
Jump to: