String comparison in Arduino [closed] - arduino

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
This code asks the user for a colour (red,green or yellow) and turns the LED on.
However for some reason, it doesn't work. All the confitions turn out to be false even when i put the correct values (red ,yellow or green)
I don't know what the reason is for this code to not work...
enter code here
void loop() {
// put your main code here, to run repeatedly:
Serial.println(Msg1);
while(Serial.available()==0){}
Val=Serial.readString();
if (Val=="red") {
digitalWrite(redLed,HIGH);
digitalWrite(greenLed,LOW);
digitalWrite(yLed,LOW);
} else if (Val=="green"){
digitalWrite(redLed,LOW);
digitalWrite(greenLed,HIGH);
digitalWrite(yLed,LOW);
} else if (Val=="yellow") {
digitalWrite(redLed,LOW);
digitalWrite(greenLed,LOW);
digitalWrite(yLed,HIGH);
}
}

Adding Val.trim() after readString will remove whitespace and your program will run as expected.

Related

How to solve the unexpected '}' in "}" in a user-made function contaning an ifelse statement? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So I have this function:
library(worms)
library(dplyr)
worms_from_species_list<-function(file_name,file_location,header=T){
if(header==T){
setwd(file_location)
lista<-read_tsv(file_name)
names(lista)<-"Species"
worms_df<-wormsbynames(lista[,"Species"])
}else{
setwd(file_location)
lista<-read_tsv(file_name)
worms_df<-wormsbynames(lista)
}
worms_df<-worms_df%>%
mutate(new_name = ifelse(status=="unaccepted" & !is.na(status),valid_name,
ifelse(status=="accepted" & !is.na(status),name,name))
return(worms_df)
}
However, I can't even run it to begin with because it returns the following errors:
Error: unexpected ')' in:
" worms_df<-worms_df%>%
ifelse(status=="accepted" & !is.na(status),name,name))"
> }
Error: unexpected '}' in "}"
Does anyone have any idea what it might be?
Most IDEs provide paren-matching in some form. In this case, it suggests that you need to close out the mutate call.
In RStudio, for instance, when the cursor is to the right of name)), it indicates that the last right-paren on that line matches the left-paren of ifelse:
The | is the cursor, and the background-gray ( is the matching left-paren.
Similarly (though it's less commonly used), emacs/ess can show it if configured as such:
Here, both the right-paren and its matching left-paren are highlighted with a blue background.
Another trick in RStudio (and other IDEs) is to indent rows and see where they pan out. You'll see that I did that in before the first screenshot above, and you can see that it thinks return(worms_df) is within the mutate.
The solution looks like this:
worms_from_species_list<-function(file_name,file_location,header=T){
if(header==T){
setwd(file_location)
lista<-read_tsv(file_name)
names(lista)<-"Species"
worms_df<-wormsbynames(lista[,"Species"])
}else{
setwd(file_location)
lista<-read_tsv(file_name)
worms_df<-wormsbynames(lista)
}
worms_df<-worms_df%>%
mutate(new_name = ifelse(status=="unaccepted" & !is.na(status),valid_name,
ifelse(status=="accepted" & !is.na(status),name,name)))
return(worms_df)
}

How to use value of a variable from one R file into another [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a config.R file which has variable P.
P_val<-function(s){
if(s==1){
return(p<-0.01)
}
else if(s==2){
return(p<-0.031)
}
else if(s==3){
return(p<-0.001)
}
else if(s==4){
return(p<-0.021)
}
else if(s==5){
return(p<-0.1)
}
else if(s==6){
return(p<-0.77)
}
else if(s==7){
return(p<-0.35)
}
else if(s==8){
return(p<-0.66)
}
}
In my main.R file I want to use this P value but the thing is this p variable here is in a loop and I want different value for each loop run. I am showing you a sample demonstration of what I want:
d<-function(num){
for(s in seq(1,8,1)){
x=2*s ##some variable
source("config.R")
P_val(s)
reset(x,p)
}
reset<-function(x,p){
l_val= (x/p) * num
return(l_val)
}
}
I am using source("config.R") in my main.R file but I don't know how to use it as I am getting this error
Error in reset(x, p) : object 'p' not found
You have to assign the value p, like so:
p <- P_val(s)
More explanations on the files and relations between i and p might be helpful here.
Assuming there is a relation between the iteration number i and value of p you can try defining this relation as a function within the config.R file.
Then you just need to source("config.R") wihtin your main.R, as you're already doing, and make a call to the function from within the iterator whenever needed.

How to transfer lines from a .txt file into an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hey guys I need to make an application using Qt that reads integers from a text file and creates a 2d plot from it. I figure that I need to transfer the integers from the txt file to an array but I am not sure how to do so.
The file has each integer on its own line. Any help will be appreciated.
Well reading a file line by line and put the contents into a vector shouldn't be that hard. Have a look at the QFile api docs. They have some basic examples there.
std::vector<int> here_are_your_ints;
QFile file("yourFileWithIntegers.txt");
// basic error checking
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
// read the contents line by line
while (!file.atEnd()) {
QByteArray line = file.readLine();
QDataStream ds(line);
int int_in_line = 0;
ds >> int_in_line;
here_are_your_ints.push_back(int_in_line);
}
Searching for a good plotting library? I am using QCustomPlot.

Find the value of a variable and replace it with new value in UNIX [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to access a text file and read some of its contents to change them with a new value.
This Find and Replace is really helpful but my requirement is slightly different.
Say if these are my file contents
image.description=Template Image 08182015
image.version=1.3.111
baseline.name=001_08_18_2015
I want to change them to
image.description=Template Image 08192015 #19 instead of 18
image.version=1.3.112 #112 instead of 111
baseline.name=001_08_19_2015 #19 instead of 18
At any point of time I will not be knowing what is the value of each variable but all I know is the variable name like "Image version" So now I need some script to find what is the value of variable image.version and auto increment the value to the next possible integer.
Any suggestions/ thoughts?
With GNU awk for the 3rd arg to match():
$ awk 'match($0,/(image\.version.*\.)(.*)/,a){$0=a[1] a[2]+1} 1' file
image.description=Template Image 08182015
image.version=1.3.112
baseline.name=001_08_18_2015
For the others since no arithmetic is involved you should be able to just use gensub() similarly.
Or, maybe this is the kind of thing you're looking for:
$ cat tst.awk
BEGIN { FS=OFS="=" }
$1=="image.description" { match($2,/.* ../); $2=substr($2,1,RLENGTH) substr($2,1+RLENGTH,2)+1 substr($2,3+RLENGTH) }
$1=="image.version" { match($2,/.*\./); $2=substr($2,1,RLENGTH) substr($2,1+RLENGTH)+1 }
$1=="baseline.name" { split($2,a,/_/); $2=a[1]"_"a[2]"_"a[3]+1"_"a[4] }
{ print }
$ awk -f tst.awk file
image.description=Template Image 08192015
image.version=1.3.112
baseline.name=001_08_19_2015

Is there a formatter/beautifier for NGINX config files? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I know this may sound a little stupid, but one of my NGINX config files is a piece of crap when it comes to formatting. It works and all but that's about it.
I tried to find some kind of beautifier or formatter, like http://jsbeautifier.org/ but then for nginx config files instead of javascript, but no luck so far.
I hope anyone would have a suggestion. There are no requirements, as long as it can format quickly / lazily made NGINX config files!
Thanks!
I found a few projects which might suit your needs:
Nginx Formatter (python) by 1connect
you can get it here
Nginx beautifier (js/nodejs) by vasilevich
nginxbeautifier.com which lets you format configs quickly in a web browser.
you can get a command line tool also on the same site to run it locally.
If your block lines end with {'s and }'s, this simple indenter could help you a bit.
It does not format all your configs, it only fixes indentation.
Original in awk (source):
#!/usr/bin/awk -f
{sub(/^[ \t]+/,"");idx=0}
/\{/{ctx++;idx=1}
/\}/{ctx--}
{id="";for(i=idx;i<ctx;i++)id=sprintf("%s%s", id, "\t");printf "%s%s\n", id, $0}
Or rewritten in python:
INDENT = ' ' * 4
def indent(contents):
lines = map(str.strip, contents.splitlines())
current_indent = 0
for index,line in enumerate(lines):
if (line.endswith('}')):
current_indent -= 1
lines[index] = current_indent * INDENT + line
if (line.endswith('{')):
current_indent += 1
return ('\n').join(lines)
There is a fork of http://jsbeautifier.org/ for nginx here: https://github.com/vasilevich/nginxbeautifier

Resources