Quantcast
[ 3 / biz / cgl / ck / diy / fa / g / ic / jp / lit / sci / tg / vr ] [ index / top / reports / report a bug ] [ 4plebs / archived.moe / rbt ]

Maintenance is complete! We got more disk space.
Become a Patron!

/sci/ - Science & Math


View post   

[ Toggle deleted replies ]
File: 2.74 MB, 3264x2448, Arduino.jpg [View same] [iqdb] [saucenao] [google] [report]
11309710 No.11309710 [Reply] [Original] [archived.moe]

Dear C/C++ Chads,

I need your help with a little problem I'm having in my research project because I'm retarded and know little about how to talk to computers.

Hey guys, I'm making a temperature-regulated water tank. There's a heater and a temperature probe, and long story short, the code is written so that if the temperature gets over 37 degrees, the heater turns off, but remains on otherwise to regulate it.

Here's my problem:

The temperature probe will sometimes report a temperature of -127 C. I'm not sure what error this stands for but we believe it's linked to power fluctuations or just errors made in communication along the way. We do not want to display this error at all.

My idea is to code the Arduino to go as follows:

>Request temperature
>If temperature = -127, Request temperature again.
>If temperature > 37, turn heater off
>Else, turn heater on

Running the code like this, it still displayed the occasional error. I'm wanting to communicate that if the temperature is -127, it does NOT report that and does not continue until it gets a non-"-127" temperature. Do not pass GO sort of deal.

What could I implement in the code to bar the code from proceeding until it gets an appropriate temperature?

>> No.11309714
File: 343 KB, 400x250, .3.gif [View same] [iqdb] [saucenao] [google] [report]
11309714

>What could I implement in the code to bar the code from proceeding until it gets an appropriate temperature?
moar bars

>> No.11309716

>>11309714
I just rammed one of the metal legs of my chair through the arduino. This did not help. WTF did you mean anon?

>> No.11309722

Post your code retard

>> No.11309724
File: 16 KB, 240x249, Oh_f04a28_481374.jpg [View same] [iqdb] [saucenao] [google] [report]
11309724

>>11309716
>I just rammed one of the metal legs of my chair through the arduino
>WTF did you mean anon?
lol not that we gotta sort this out OP oh man

>> No.11309725

>>11309722
https://pastebin.com/QbXPqAxb
There it is. Sorry if it's an abomination.

>> No.11309726

>>11309725
Let me post another one soon with notes for everything so it's comprehensible.

>> No.11309727

>>11309725
>https://pastebin.com/QbXPqAxb
hahahaha what the fuck is that

>> No.11309731

>>11309727
I realized after I posted it how disgusting it must be.

>> No.11309736

>>11309731
why are you requesting the fucking temperature so many times in the loop? why don't you just request it once at the top, do something based on that number and reduce your 5 second delay?

>> No.11309742

>>11309736
1. Having it take the temperature like three times was a last-minute guess I made that I haven't tested yet, I figured maybe that doing it multiple times would check the code if it reported multiple errors in a row.
2. Honestly though, I don't know why I haven't tried that yet. This is my first actual coding project so it's probably something I didn't think to do while putting it all together.
3. The delay is set to five seconds because there is a pump that is being powered at the same time as the heater. When the heater turns on, the pump's power is reduced, and it can get annoying to have that fluctuate every second or two. Five seemed to be fine. Though it may be best to reduce the delay.

>> No.11309755

use an unsigned int

>> No.11309758

>>11309742
1. Check the temperature one time at the top, put it into a double variable. You can reduce this all to like 10 lines of code dude

2. Check your serial output to see exactly what value you're getting when you see the error, if it's genuinely 137 you can add an if statement that checks if it's 137 and if so, immediately restarts the loop (via the continue command). It would probably be good for you to serial print all the values you see so you know the exact ranges, but whatever.

3. Add an if statement after that that checks if it's over 37 degrees, if so, turn the heater off. THIS is where you should put your delay if anything. You also need a global variable with the STATE of your heater so you can avoid turning it off if it's already off, turning on if it's already on etc. This should be changed when you turn it on or off and checked before you turn it on or off. At the end of this if statement you could add another continue to go to the end of the loop.

After this if statement you could just have a default "if heater is not on, turn heater on" block that will only ever get activated if the temperature isn't 137 and isn't above 37.

>> No.11309761

>>11309758
I haven't worked with double or global variables yet, but I'm assuming these are things I can google to find out more about them.

Thanks so much anon. I'll try this out. Also, for the serial data, is that still stored on the Arduino itself or is that reported to the computer via USB? Because I'm running this off of an external power supply and found that if I have it hooked up like that and to my computer at once, there are far more errors and it messes up the readings.

>> No.11309778

>>11309761
Via USB is the easiest way, you shouldn't be getting any errors connecting it via USB. You can use the Tx pins to send that serial data to an LCD or something but usb is way easier. Your code could roughly look something like this

Loop Start
Temperature = checkTemperature

if Temperature == 137
->Continue

if Temperature > 37 && heaterState == true
->heaterState = false
->turnOffHeater()
->delay(1000)
->continue
elseif Temperature <= 37 && heaterState = false
->heaterState = true
->turnOnHeater()
->delay(1000)
->continue

End loop

>> No.11309796

>>11309761
Next time look into tutorials by vaguely typing in your intended goals into google. Arduinos are popular because of the endless content, resources and support out there and all the stuff you need to do for this is contained in the very very basic tutorials. Getting your serial monitor up for debugging/monitoring is pretty key in that.

>> No.11309799

>>11309796
Brilliant help. I'll be sure to do that, too. Thank you, King

>> No.11309801

>>11309710
>>11309755
Assuming its an 8 bit signal, -127 means all 8 of the measurement bits are 1. If he went unsigned he'd just get +255 degrees.

OP you could take a running average on the past N-second window and not count the erroneous readings towards the result.

>> No.11309835

>>11309725

> The character "{" appears 32 times
> The character "}" appears 28 times

That's what you get for using tarduino.

>> No.11309849

>>11309835
I think that's what I get for being a retard. This is embarrassing.

>> No.11310032
File: 46 KB, 224x211, modern_great_engineer.png [View same] [iqdb] [saucenao] [google] [report]
11310032

>>11309710
not gonna lie, code's pretty gross but if it works it works, i've submitted half baked piles of garbage before and I've gotten A's

>>
Name (leave empty)
Comment (leave empty)
Name
E-mail
Subject
Comment
Password [?]Password used for file deletion.
reCAPTCHA
Action