![]() |
Nerd homework
My son needs help with his homework and I know nothing about coding , he is trying to get a RGB strip to light up with increasing intensity as more pressure is applied to sensor while also randomizing a series of colors .(llike a light up shoe)
this is what he has which he knows is horribly wrong i am pretty sure this is C/C+, this is his hisrt ever coding class so his knowledge is fairly limited. I also know it is in french but it might still make sense to some of you int valeur1 = 0; //variable de type integer pour enregistrer analog A0 int valeur2 = 0; //variable de type integer pour enregistrer analog A0 int valeur3 = 0; int valeur4 = 0; int valeur5 = 0; void setup() { Serial.begin(9600); //commence le serial pour ecrire les données pinMode(A0,INPUT_PULLUP); //prepare A0 pour le pressure sensor pinMode(3,OUTPUT); //prepare pin 3 pour output de 5V pinMode(5,OUTPUT); pinMode(6,OUTPUT); } void loop() { valeur1 = analogRead(A0); delay(1000); valeur2 = (200/valeur1); valeur3 = (valeur2*valeur2); valeur4 = random(255); valeur5 = valeur4; if (valeur4 > valeur5){ valeur5--; analogWrite(3,valeur5); } if (valeur4 < valeur5){ valeur5++; analogWrite(3,valeur5); } if (valeur4 == valeur5){ valeur5; analogWrite(3,valeur5); } if (valeur4 > valeur5){ valeur5++; analogWrite(5,valeur5); } if (valeur4 < valeur5){ valeur5--; analogWrite(5,valeur5); } if (valeur4 == valeur5){ analogWrite(5,valeur5); } if (valeur4 > valeur1){ valeur5++; analogWrite(6,valeur5); } if (valeur4 < valeur5){ valeur5--; analogWrite(6,valeur5); } if (valeur4 == valeur5){ analogWrite(6,valeur5); } } would appreciate any help or insults |
|
quick search, might help...
https://learn.sparkfun.com/tutorials...okup-guide/all good luck, google/bing/duck duck go... ect are you friend went to comes to code, everything is online, (Mostly)... |
Quote:
|
This looks racist. Is it racist?
|
Quote:
|
Quote:
|
It would help to see the formal requirements of the assignment.
|
The kid needs to learn english. I can't understand a word he's saying.
|
is it just me or is high school a lot more difficult these days?
have no idea what's happening in the OP |
Canada must be stopped!
|
Clean up the God damn brackets. Jesus. Heathen.
|
don't speak French and don't code in c++ but So I can see him declaring Integer variables then passing them to a function then based on their return, passing that to another function which I assume returns the color.
Would help if I need what the functions do exactly but seems like he is on the right track at least with the flow. I mostly code in c#. Sent from my Pixel 3a using Tapatalk |
Be sure to drink your Ovaltine.
|
Also the ifs in c# would look like
If (this) { Than that } Else if (this and this) { That and this } Else { } Anyway that's how I'm used to seeing multi if statements in my world Also he has val 5 equals val 4 and then is comparing them in the if statements? They are the same. Sent from my Pixel 3a using Tapatalk |
Have you tried turning it off and back on again?
|
Quote:
|
Wtf is a Ruth Bader ginsberg color?
|
Needs a lot of additional information. No offense, but it kinda looks like your son found a bit of code online and is trying to work backwards without knowing what it means. The code just declares some variable values, but a lot seems to be missing to actually do much of anything. Would need more info on what RGB board model he's using and so on.
|
He shouldn’t try to write the entire code all at once. It can be too hard to hold the whole logic flow in his head all at once, even for a small problem like this. Instead, he should break it down into simpler projects that will contribute to the larger solution.
1. Turn an rgb strip on. 2. Make an rgb strip show random colors. 3. Make an rgb strip turn on only when the touch pad is touched. 4. Make an rgb strip intensity responsive to pressure. 5. Combine 2 and 4. Done. This process of breaking problems down into smaller manageable parts and then composing the big solution from smaller solutions is a very important skill for engineering or software. As a budding engineer, he should focus on one tiny task in isolation of the others and just keep pounding on it until he gets a breakthrough. Most likely he will find the activity of solving small problems all consuming. This will be the process he follows his entire career. If he tries to grapple with the big problem all at once, it can get confusing and frustrating very fast. Imagine trying to design a whole commercial airplane all at once. Every engineer breaks complicated problems into smaller, manageable parts and knocks them down one at a time. |
Quote:
It is arduino uno is the item he is working with. |
Quote:
I learned this in college when I'd write a bunch of code, then would get so frustrated with it I'd start deleting shit until I found the dumbest bullshit ever that I would have found if I had only written the first few lines and tested. I would also suggest outputting status and variables along the way... so when he sets valeur4 to a random number, add a line to echo that so you know what that value is, and then in the first if statement, have it output "in the first if statement", valeur4=(whatever), valeur5=(whatever).... to ensure it's doing what you expect it to as you follow the wisdom above and go step by step. |
I'm guessing this is an Arduino 'Sketch' script. Reference for that language syntax can be found here: https://www.arduino.cc/reference/en/ in English even! And yes, it's basically C++ with a little special sauce added in.
|
Quote:
|
Quote:
|
Quote:
Disclaimer, I didn't stay at a Holiday Inn Express last night but I have taught object oriented programming at a state university for almost 10 years. |
Quote:
In setup, you initialize your variables, pins, etc. In loop, you put in the code that will repeatedly execute in a loop. Considering the stated objectives, I'd solve the problem in the loop code like so: * Read the pressure sensor value and convert that to a decimal value between 0 and 1. * Generate random values for red, green and blue LEDs and store those values into three variables. * Multiply each of those three variables by the converted pressure sensor value. * Write the three values to the RGB LED output pins. |
Quote:
I have been programming on a regular basis for more than 40 years. I built from scratch a website that contains more than 30,000 lines of code. Even with my experience, when I started fiddling with arduino boards (which sounds like what your son is working with) a few years ago out of curiosity, I would have broken the problem your son is working on now into these tiny mini projects. Literally some of the first things I did with the arduino were to turn on an led, turn on an led with a switch, turn on an led for random periods of time, etc. My goal project was to time how long it took for weighing lever to tip using an accelerometer, and combine that information from a level sensor to calculate the velocity of water flow in a channel. This had nothing to do with leds being on or off. But because I was working in a new ecosystem, I started with the most basic of baby steps. When I built my goal project, it involved maybe half a dozen sub projects along the way. When I fully understood each sub project, composing them into the final project was fairly easy because of the familiarity I had built up working on the sub projects. |
Lots of good advice here. I think cdcox and I may have a lot in common. I too have been programming for a long time. Started at 14 yrs old on an Atari 800. That was a few years ago!
Ok, so I dug out my last ESP32 project. If you are using the Arduino IDE, which I would assume you are, you can print variable values to the console by using "Serial.println()". So for instance, if you are trying to see what the actual value of your pressure sensor is, you could do something like this: Code:
pressureValue = analogRead(A0); |
Appreciate the help guys , not sure if he figured it out i went to bed but will let you know
|
If you need a poll rigged, a mom to bang, or even code, your first stop shop is CP!
|
Quote:
|
Billay's is available anytime
|
Do you have any specs on the RGB strip and how the host system is connected to it (what are the three output pins connected to on the RGB strip)?
Edit: I guess it doesn't really matter. I'd start by just reading the analog input and putting that value on the three outputs and verify that he can see a color change based off the pressure input. I think it's been said a few times in this thread already but he needs to break it down into smaller chunks. |
Quote:
|
I'm not much into coding but the following doesn't make much sense.
Quote:
|
Quote:
Also (and I've never worked with this Arduino stuff), since he already is setting up the serial port for comms, will Serial.print work as someone suggested? Or will that output over the current serial port link? |
Quote:
|
Have him change to something other than coding. It looks like french
|
Quote:
When doing Arduino programming and debugging, the serial line is how you write and read data from the device as it is operating. In essence, your IDE compiles down the C++ sketch file into a binary, uploads that over the serial line to the Arduino device, and then resets it. The reset makes it execute your code. The serial connection remains open, and any messages from the Arduino are sent across. |
Quote:
Normally I would spend the next few months and hundreds of hours learning some coding to be able to help him out but I have absolutely no interest . |
I’m not a coder, (although I am an engineer by education) but I do know Arduino the company extremely well.
Where you want to go is the Arduino forum https://forum.arduino.cc/ There are 1M members and you are likely to find plenty of good ideas and assistance. |
This is a little difficult given the lack of context to the code. I'm assuming the outputs 3,5 & 6 are in relation to RGB color. If not disregard this message. I don't see how you would add intensity based on the pressure sensor unless it is saying pin 3 determines power. IF that is the case we want to tie that to valuer1 and I would have to have more info on how the colors are determined.
I rewrote the loop for what I think would give you a random changing light every second, but without more information on the board with regard to setting intensity, I can't help you there. void loop() { valeur1 = analogRead(A0); delay(1000); valeur2 = (200 / valeur1); //need context on what these are for valeur3 = (valeur2 * valeur2); //need context on what these are for valeur4 = random(255); // three random values. One for each RGB. If you use only one you will be limited to 255 colors. valeur5 = random(255); valeur6 = random(255); analogWrite(3,valeur4); analogWrite(5,valeur5); analogWrite(6,valeur6); } |
This shit sure ain't assembler
|
I just hope this kid renames the variables before turning in the code. If I were grading the assignment, I'd be raising an eyebrow if the variables were French. ROFL
I once worked on some client's code, which was apparently written by a guy named Eric. Every variable in his code was prefixed with 'eric'. So the SQL DB connection was 'ericDbConn', and the result set was 'ericDbResult', the datarow object was 'ericDbRow', etc. I thought that was pretty damned hilarious! |
Quote:
|
Curly brackets?
Apparently it's been awhile since I dipped my toe in C world so I'll just say make the code more linear and unless he's coding for a French person use the English Language for comments. |
Quote:
I recall that the guy left a lot of comments too, which is usually a good thing, but his comments were unnecessary, as they didn't explain anything helpful. Example: Code:
// This is the database connection |
Quote:
|
Quote:
-- Phil Karlton https://martinfowler.com/bliki/TwoHardThings.html |
Quote:
|
Quote:
thanks fellas . |
Quote:
Glad to hear that this helped. Is your son looking to pursue a career in computer science, or is this just one of his classes? Programming can be intimidating at first, but if he sticks with it, things will become easier. All that gibberish starts to make sense at some point. |
Quote:
|
Not sure how many more labs he will have, but here's a piece of advice our instructors gave us.
When you're trying to work out a problem with your code, step away. Do something else, get your mind off of it. Sometimes clearing your mind will bring you the answer. Also when you go to bed, put a pad and pen next to your bed. You would be surprised how many times that worked for me, waking up with either an answer to a problem or an idea of how to write a program in the middle of the night. Good luck. |
Quote:
|
All times are GMT -6. The time now is 11:45 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.