I cannot get a function for this code to really work.
lastVal = val;
val = digitalRead(DT);
if (val == 1 && lastVal == 0)
{
if (digitalRead(CLK) == 1)
{
pos++;
}
else
{
pos--;
}
}
Can somebody pleas help me?
I am not sure if this is your entire code or not, but in case that is all of the code then I know the reason. Arduino requires the basic setup and loop functions to be referenced in the code, as long as it is referenced you should be fine - you can even leave the inside of the functions empty. You have not really asked the question very well so it is hard to see what you mean.
To create a function you can use this code:
void function_name_here(_parameters_here_)
{
//Code Here
}
To reference that function you just declare it by using:
function_name_here();
By the looks of it you might want to put your code in to the loop function, your code might look like this:
int DT = /* Value here */;
int pos = /* Value here */;
void setup()
{
pinMode(DT, INPUT);
}
void loop()
{
lastVal = val;
val = digitalRead(DT);
if (val == 1 && lastVal == 0)
{
if (digitalRead(CLK) == 1)
{
pos++;
}
else
{
pos--;
}
}
}
Related
I want to build an application that calculates large computations using an idle timer. The progress bar works fine for small numbers but if I enter large numbers as input the progress bar does not display anything.
Code, where I set the QProgressbar, is as shown below.
void MainWindow::on_startButton_clicked()
{
numberEntered = ui->inputValue->text().toInt();
QIntValidator * validator = new QIntValidator(this);
ui->inputValue->setValidator(validator);
ui->progressBar->setMaximum(numberEntered);
globalIndex = 0;
timer->start(0);
ui->resultArea->append(QString("STARTED"));
}
and in another place
void MainWindow::countManager()
{
if(globalIndex == 0){
ui->resultArea->append(QString("%1 : is a Prime number").arg(globalIndex));
ui->progressBar->setValue(globalIndex);
}
if(globalIndex <= numberEntered && globalIndex != 0){
int result = primeNumberCalculator(globalIndex);
ui->progressBar->setValue(globalIndex);
if(result !=0){
ui->resultArea->append(QString("%1 : is a Prime number").arg(result));
}
}
if(numberEntered == globalIndex){
timer->stop();
ui->resultArea->append(QString("End of count!!"));
}
globalIndex++;
}
I have a menu function, in which I input a question and two options, then the user choses one. It works just fine everytime but one ; I call
if (menu("ou est le corps?","interieur ","exterieur")==1)
{
but instead of printing "interieur " It shows "p?"
it works just fine without the space, but I need to make a space and \n does quite the same thing.
I have another call of this function, with \n which works fine so I have no idea about why this wouldn't work. Anyone has got an idea?
PS : the value of choix1 is then sent via bluetooth, and there it stays intact.
PPS : tell me if something is unclear, I'm not naturally english
PPPS(sorry) : tried to run the same code again, it seems to print a random character followed by "?", I had twice "p?", once "?" and once " '?"
[updates] once "#?"
int menu (String texte, String choix1, String choix2)
{
envoye = 0;
rxValue = "0";
while (digitalRead(M5_BUTTON_HOME) != LOW && rxValue == "0")
{
heure();
M5.Lcd.setTextSize(2);
M5.Lcd.print(texte);
M5.Lcd.printf("\n");
if (selec == 0)
{
M5.Lcd.printf("->%s %s", choix1, choix2);
}
else
{
M5.Lcd.printf(" %s ->%s", choix1, choix2);
}
if (M5.BtnB.read() != 0)
{
if (selec == 0)
{
selec = 1;
}
else
{
selec = 0;
}
while (M5.BtnB.read() != 0)
{
if(digitalRead(M5_BUTTON_HOME) == LOW)
{
M5.Lcd.fillScreen(BLACK);
delay(1000);
if(digitalRead(M5_BUTTON_HOME) == LOW)
{
choix=50;
heure();
delay(1000);
return 1;
}
}
}
}
if (deviceConnected && envoye == 0)
{
sendchoix(texte, choix1, choix2);
envoye++;
}
}
if (rxValue != "0")
{
recuble = &rxValue[0];
selec = atoi(recuble) - 1;
rxValue = "0";
}
M5.Lcd.fillScreen(BLACK);
delay(300);
return selec;
}
int menu (String texte, String choix1, String choix2) {
[...]
M5.Lcd.printf("->%s %s", choix1, choix2);
You cannot treat String objects as const char*, which is what the format specifier %s is expecting. String is an Arduino class for storing.. strings/character data, but an object of this class is not equivalent to the raw pointer to the data.
For that, you need to call the c_str() method on the String object to get the C-String pointer to the data, as shown in the documentation [1].
[..]
M5.Lcd.printf("->%s %s", choix1.c_str(), choix2.c_str());
[..]
[1] https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/c_str/
I want to do something like this:
int _radioValueGender == _radioValueGender
void setRadioValueGender(DocumentSnapshot document) {
if (document['gender']=='female') {
return _radioValueGender == 0;
}
else {
return _radioValueGender == 1;
}
}
Where I set the value of an integer _radioValueGender depending on the result of an if/else statement. I would be really appreciative of any help. I hope that this is easy, it just seems to be a question of knowing the right methods. I apologize for any inconvenience and thanks in advance.
== is the equality comparison operator. It does not do variable assignment.
setRadioValueGender is declared to have a void return type. It is incorrect to return values from it.
What you want is:
void setRadioValueGender(DocumentSnapshot document) {
if (document['gender']=='female') {
_radioValueGender = 0;
} else {
_radioValueGender = 1;
}
}
or more concisely:
void setRadioValueGender(DocumentSnapshot document) {
_radioValueGender = (document['gender'] == 'female') ? 0 : 1;
}
Simply modify your initial int _radioValueGender == _radioValueGender declaration and make it int _radioValueGender;. That way it forces the value to be any int object.
Then instead of result use setState(() {_radioValueGender =
0 }); in order to modify your variable anytime 'setRadioValueGener' has been pressed.
== is a comparison, when you type that you are asking if the two values on the left and the right are equal. = is an assignment, when you type it you are assigning the value on the left to the right. so just change it to this:
void setRadioValueGender(DocumentSnapshot document) {
if (document['gender']=='female') {
return _radioValueGender = 0;
}
else {
return _radioValueGender = 1;
}
}
I want to change the function inserted at the timing before the instruction with the same address.
What should I do?
For,example.
int count=10;
void insert_check_code(INS ins){
if(INS_Address(ins) == tmpaddr)
if(count > 5){
INS_InsertCall(ins,IPOINT_BEFORE,count--func)
}else {
INS_InsertCall(ins,IPOINT_BEFORE,count_printfunc)
}
}
In the above example,The count value returns to its original I want to change the function inserted at the timing before the instruction with the same address.
What should I do?
For,example.
int count=10;
void insert_check_code(INS ins){
if(INS_Address(ins) == tmpaddr)
if(count > 5){
INS_InsertCall(ins,IPOINT_BEFORE,count--func)
}else {
INS_InsertCall(ins,IPOINT_BEFORE,count_printfunc)
}
}
In the above example,The count value returns to its original value.
The target program is a simple server program, so we are using the fork () function.
Is it necessary to write a special description in Pintool for a program using the fork () function?value.
Move the if-else code into its own function and insert that function instead:
void conditional_func() {
if(count > 5){
count_decrement_func()
} else {
count_printfunc()
}
}
void insert_check_code(INS ins) {
INS_InsertCall(ins,IPOINT_BEFORE,conditional_func)
}
Regarding fork(), it depends on what behavior you want when fork()ing.
Keep in mind that if the application is multi-threaded, count access must be synchronized.
Consider these C functions:
#define INDICATE_SPECIAL_CASE -1
void prepare (long *length_or_indicator);
void execute ();
The prepare function is used to store a pointer to a delayed long * output variable.
It can be used in C like this:
int main (void) {
long length_or_indicator;
prepare (&length_or_indicator);
execute ();
if (length_or_indicator == INDICATE_SPECIAL_CASE) {
// do something to handle special case
}
else {
long length = lengh_or_indicator;
// do something to handle the normal case which has a length
}
}
I am trying to achieve something like this in Vala:
int main (void) {
long length;
long indicator;
prepare (out length, out indicator);
execute ();
if (indicator == INDICATE_SPECIAL_CASE) {
// do something to handle special case
}
else {
// do something to handle the normal case which has a length
}
}
How to write the binding for prepare () and INDICATE_SPECIAL_CASE in Vala?
Is it possible to split the variable into two?
Is it possible to avoid using pointers even though the out variable is written to after the call to prepare () (in execute ())?
The problem with using out is that Vala is going to generate lots of temporary variables along the way, which will make the reference wrong. What you probably want to do is create a method in your VAPI that hides all this:
[CCode(cname = "prepare")]
private void _prepare (long *length_or_indicator);
[CCode(cname = "execute")]
private void _execute ();
[CCode(cname = "prepare_and_exec")]
public bool execute(out long length) {
long length_or_indicator = 0;
prepare (&length_or_indicator);
execute ();
if (length_or_indicator == INDICATE_SPECIAL_CASE) {
length = 0;
return false;
} else {
length = lengh_or_indicator;
return true;
}
}