Using 'if' argument to return a value

All, for a couple of projects I need the ability to compare a given value, then assign it a different value (for example a name when a number is called out)

The 'if' command looks like it should be able to do the job, but all the examples I am seeing on the web during a search are more for turning on an LED or some other physical type of output like this type

if (colorChoice=="red") {

analogWrite(redPin, brightness); //turn on red pin
analogWrite(greenPin, 0); //turn off green pin
analogWrite(bluePin, 0); //write off blue pin

What I need is to just assign a value

Any help understanding this is very much appreciated

Cheers

Les

What type of variable are you comparing?
Assign a value to what?
“Return” to where?

This question is too generic to be able to give a straight answer, other than perhaps...

if (someVariable == someValue)
{
    someOtherVariable = someOtherValue;
}

If you know how an “if” statement works, and you know what a variable assignment is, then you just combine the two? What’s the problem?

Ok if it is as simple as that then I can give it a go

The intent is to use the output from a program to define what happens, for example whether a character or picture is displayed on a screen, or a number to output to a stepper motor controller. The definition is rather generic based on the fact that I have intentions to use it on a number of things - sorry if that is unhelpful but sometimes you have to start by knowing if something is possible and get some guidance

Thanks for your input

Les

Hi Les,

With the intent of encouraging you, please always 'give it a go', what have you got to lose? Type in what you think and see if it works. I would never hesitate to try some code out, you won't break anything by typing incorrect code, but you will learn something!

PerryBebbington:
you won't break anything by typing incorrect code

[ripple effect on screen for flashback to c1971, school visit to ICL HQ....]
The guy showing us around was explaining how computers are so dumb and do what they're told, which is not necessarily what the human wants. His actual example was if you have the greater than sign back to front and mean to check if the bath water is under the right temperature before adding hot water, but actually check if it's over, you will add more and more hot water making the temperature ever hotter so you'll add more hot water...
[ripple effect forward 50y...]

So it's naive to say you won't break anything if the code's wrong; what if the motor's turning the wrong way and drives the robot into the wall not away.

That's why we have test environments, so that broken code just flashes the wrong led or prints the wrong message, but doesn't boil someone.

Even in ArduinoLand we can do that: make it so the test identifies the fault before it's attached to the plant.

Thanks Perry

So far my biggest roadblock in all this is when it comes up with the 'function-definition is not allowed here before '{' token' or 'XX' was not declared in this scope' errors, as at my level of competence I have real trouble in resolving them. This means that when I hit one of them I have to seek assistance, so 'just tying things' gets slowed down and timed by the cadence of the replies. Until I get past it everything stops

When you ask a question, unless it is very code specific, the guys on the other end are very unlikely to have the same hardware and software, and are not intimately acquainted with your requirements - tough for them to advise in that case

Plus of course people inevitably and understandably get tired of fielding those sort of questions repeatedly. I've printed off a load of stuff to read up on some of the errors, but it's not easy to understand!

Jubukraa, I know what you mean but in my case it is a very benign issue - the most I'll do is burn out an OLD module or stepper motor driver, and considering the screw ups I've done on the rest of the project, that is trivial!

Cheers

Les

Would this string be logical to use?

unsigned int digit;
if (value == 100) {

digit = 6;
{

cheers

Les

So far my biggest roadblock in all this is when it comes up with the 'function-definition is not allowed here before '{' token' or 'XX' was not declared in this scope' errors, as at my level of competence I have real trouble in resolving them.

I used to write assembly language on PICs, but it became more and more obvious that I needed to learn C. I was lucky in having 2 friends teaching me. I did however have many of the same problems you highlight, maybe not the exact same ones but simple things that cause pain in the arse errors. I know how easy it is to look at simple code and not see the blindingly obvious error. I still do it, the errors are different, but the ability to stare at them and not see them is the same. For me the solution is to go and do something else, get some sleep and come back to it later, often then the error I could not see becomes painfully visible.

When you ask a question, unless it is very code specific, the guys on the other end are very unlikely to have the same hardware and software, and are not intimately acquainted with your requirements - tough for them to advise in that case

You are correct, although some errors are obvious when you know the kinds of mistakes people generally make.

Plus of course people inevitably and understandably get tired of fielding those sort of questions repeatedly.

I don't mind answering the same simple questions, I know they are not simple for the person asking. I do mind when people are obviously not making any effort themselves and just want someone to do it for them. I am sure you have read many such instances and I am sure you have seen mine and other regular contributors' responses. To the people who don't make any effort I think Pink Floyd put it perfectly with:

Ticking away the moments that make up a dull day
Fritter and waste the hours in an offhand way.
Kicking around on a piece of ground in your home town
Waiting for someone or something to show you the way.
Tired of lying in the sunshine staying home to watch the rain.
You are young and life is long and there is time to kill today.
And then one day you find ten years have got behind you.
No one told you when to run, you missed the starting gun.
So you run and you run to catch up with the sun but it's sinking
Racing around to come up behind you again.
The sun is the same in a relative way but you're older,
Shorter of breath and one day closer to death.
Every year is getting shorter never seem to find the time.
Plans that either come to naught or half a page of scribbled lines
Hanging on in quiet desperation is the English way
The time is gone, the song is over,
Thought I'd something more to say.

Sadly there seem to be a lot of people waiting for someone or something to show them the way.

@jubukraa,
Sorry, but you have taken what I said so far out of context it's fallen off a cliff. We are not talking about the situations you mentioned, we are talking about whether someone experimenting with a micro controller and trying to work out the basics should just type something in and see what happens or ask first. I absolutely say just try it and don't worry about the consequences. An Arduino is not going to go up in smoke because of incorrect code. Maybe some LEDs will flash in the wrong order or a cup of tea will come to a sticky end against the wall in frustration, but that's about it. When someone comes to writing automated bath filling software they will know the difference between < and > because they will have made that mistake in a more benign environment, as indeed have I.

Lesthegringo:
Would this string be logical to use?

unsigned int digit;
if (value == 100) {

digit = 6;
{

There is no string in your code so I don't understand the question.

However the code should work assuming that value is also defined as an int. That's why we ask you always to post a complete program.

...R

PS ... When posting code please use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to use the forum

Lesthegringo:
Would this string be logical to use?

 unsigned int digit;

if (value == 100) {

digit = 6;
{

Please use code tags Les!
Does it do what you want it to do?
What error do you get when you discover it doesn't compile?
What do you think the cause of the error is?

Would this string be logical to use?

unsigned int digit;
if (value == 100) {

digit = 6;
{

Yes, that's quite typical of state machines for example, where your generic "digit" could be "systemState" and your generic "value" could be from some sensor. So the system's state (eg a case in a switch..case) and consequently its output would be controlled by the input.

There is no string in your code so I don't understand the question.

I think he meant string of code, as in a snippet?

An Arduino is not going to go up in smoke because of incorrect code.

I'm pretty sure we could come up with some examples of where it might. What about a pin shorted to ground, incorrectly set as a high output? That could release smoke.

(There's a recent thread about the need for a series resistor to prevent exactly that.)

I'm pretty sure we could come up with some examples of where it might. What about a pin shorted to ground, incorrectly set as a high output? That could release smoke.

I don't know what the point of this is, I am trying to encourage people to try stuff and not be afraid. You seem to want people to stop at every key stroke and ask if it's OK to proceed. If that works for you OK, it doesn't work for me and I find that approach irritating at the least.

Thanks - I knew the code thing but was just including an example in text form, but point well noted - yes, I meant string as in string of text

let's give it a whirl

Les