How to redefine a variable character for the first time in arduino - char

i have this code to Comparison two char
char pass[]="1";
char passin[100]="";
and code to put characters in variable
void loop() {
char key= keypad.waitForKey(); // read char from keypad
strcat(passin,key);
if(strcmp(pass,passin)==0)
{ lcd.clear();
lcd.print("login success");
}else
{ lcd.clear();
lcd.print("login failed");
lcd.setCursor(0,1);
}
}
delay (1000);
}
what the way to return the value of passin as define like first time

If you just want to compare one character with another you can use the "==" operator. Note that to declare a character you use single quotes e.g. 'a'.
The code can be like this:
char pass = '1';
char key;
void loop(){
key = keypad.waitForKey(); // read char from keypad
if(pass == key){
lcd.clear();
lcd.print("login success");
}else {
lcd.clear();
lcd.print("login failed");
lcd.setCursor(0,1);
}
delay(1000);
}
The character key will be set to the new value every iteration of the loop.

Related

Menu type display in LCD using 4x4 Keypad in Arduino

I am trying to display two menu type in 16x2 LCD display using keypad in Arduino.
Here is the code;
`#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
byte rowPins[4]={9,8,7,6};
byte colPins [4]={5,4,3,2};
char keys[4][4]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,4,4);
LiquidCrystal_I2C lcd(0x27,16,2);
boolean present = false;
boolean next = false;
boolean final = false;
String num1, num2; //
float ans;//
char op;//
//String Line1 = " ";
//String Line2 = " ";
//char cursors = 0;
//
////phaseshift
//char phase_value[2];
//int phase_cursor;
//int phase_index;
//char phasesign;
//
////declare function
//void Phaseshift_Fn();
//void CV_Fn();
//void CC_Fn();
//void Remote_Fn();
void setup() {
lcd.init();
lcd.backlight();
lcd.begin (16,2);
lcd.clear();
lcd.setCursor(0, 0); lcd.print(Line1);
lcd.setCursor(0, 1); lcd.print(Line2);
delay(100);
lcd.setCursor(5,0);
lcd.print("HELLO");
lcd.setCursor(0,1);
lcd.print("PRESS FWD Button");
delay(2000);
lcd.clear();
delay(1000);
}
void loop() {
char key = keypad.getKey();
if(key == 'B'){
lcd.setCursor(0,0);
lcd.print("IF NO AIR BUBBLE");
lcd.setCursor(0,1);
lcd.print("PRESS STOP");
}
if(key == 'A'){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PRESS MODE");
lcd.setCursor(0,1);
switch(mode)
{
case Startup:
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
Line1 = "Enter Amount";
lcd.print(Line1);
lcd.setCursor(0,1);
Line2 = "Enter Time";
lcd.print(Line2);
delay(1000);
mode = Mainmenu;
break;
}
if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'))
{
if (present != true)
{
num1 = num1 + key; // storing the value of key pressed in num1
float numLength = num1.length();
lcd.setCursor(0, 1); /* decaling the place where the first entry will be displayed*/
lcd.print(num1); // printing the first number entered
}
else
{
num2 = num2 + key;//storing the value of second key pressed in num2
float numLength = num2.length();
lcd.setCursor(4, 1);/*decaling the place where the second entry will be displayed*/
lcd.print(num2); //printing the second number entered
final = true;
}
}
}`
As a beginner of the Arduino programming I tried so many methods including switch methods also.
But it didn't display. Please anyone can help me to solve this code?
I am trying to display this;enter image description here
Here I want to display
when I click the S button there two menus are displaying. They are Stop and Start. I want to display that two in LCD and if start button click then machine is forward after that when I click the stop button it should be stopped.

Why is this code breaking on string input?

#include <iostream>
using namespace std;
string flip();
int x;
int main()
{
for(int i=0;i<10;++i){
cout<<"Press 1 and enter"<<endl;
cin>>x;
if(isalpha(x)==true){
cout<<"int pls"<<endl;
}
else if(x==1){
flip();
cout<<"flipped"<<endl;
}
else if(x!=1){
cout<<"try again"<<endl;
}
}
system("pause>0");
return 0;
}
string flip(){
string ans;
int y=rand()%2;
if(y==0){
string ans = "Heads";
cout<<ans<<endl;
}
else{
string ans = "Tails";
cout<<ans<<endl;
}
return ans;
}
whenever I put 2 instead of 1 it works and says try again but when i write some string like "fa"
the code closes instead of writing try again
if I change x to int and then if I try to input some string it just prints press 1 and enter 10 times instead of asking for an input again
The type of variable 'x'(=The argument of isalpha()) has to be char. If the type of 'x' is integer, the function(=isalpha()) recognizes 'x' as an ASCII value. How about trying this code?
char x;
int main()
{
for(int i=0;i<10;++i){
cout<<"Press 1 and enter"<<endl;
cin>>x;
if((bool)isalpha(x)==true){
cout<<"int pls"<<endl;
}
else if(x=='1'){
flip();
cout<<"flipped"<<endl;
}
else {
cout<<"try again"<<endl;
}
}
system("pause>0");
return 0;
}

Adding custom environment to CreateEnvironmentBlock()

I'm using CreateProcessAsUser + CreateEnvironmentBlock to copy environment variables from another process.
Is it possible to add custom environment variable to the result of CreateEnvironmentBlock call?
The environment block is just a series of null terminated strings (ending with a double-null terminated string). Just parse it, insert your new key/value pairs, and save back to a buffer. I wrote some code to get you started:
wstring lowercase(const wstring& s)
{
std::wstring str(s);
std::transform(str.begin(), str.end(), str.begin(),
[](wchar_t c) { return std::tolower(c); });
return str;
}
wstring appendToEnvironmentBlock(const void* pEnvBlock, const wstring& varname, const wstring& varvalue)
{
map<wstring, wstring> env;
const wchar_t* currentEnv = (const wchar_t*)pEnvBlock;
wstring result;
// parse the current block into a map of key/value pairs
while (*currentEnv)
{
wstring keyvalue = currentEnv;
wstring key;
wstring value;
size_t pos = keyvalue.find_last_of(L'=');
if (pos != wstring::npos)
{
key = keyvalue.substr(0, pos);
value = keyvalue; // entire string
}
else
{
// ??? no '=' sign, just save it off
key = keyvalue;
value = keyvalue;
}
value += L'\0'; // reappend the null char
env[lowercase(key)] = value;
currentEnv += keyvalue.size() + 1;
}
// add the new key and value to the map
if (varvalue.empty())
{
env.erase(lowercase(varname)); // if varvalue is empty, just assume this means, "delete this environment variable"
}
else
{
env[lowercase(varname)] = varname + L'=' + varvalue + L'\0';
}
// serialize the map into the buffer we just allocated
for (auto& item : env)
{
result += item.second;
}
result += L'\0';
auto ptr = result.c_str();
return result;
}
Then let's say you want to insert "NewEnvironmentVariable=GoSeahawks!!!" into the environment block. Invoke as follows:
::CreateEnvironmentBlock(&envblock, hToken, TRUE);
wstring updatedBlock = appendToEnvironmentBlock(envblock, L"NewEnvironmentVariable", L"GoSeahawks!!!");
CreateProcessAsUser(token, ..., updatedBlock.c_str(), ...);

arduino "error: expected unqualified-id before '=' token"

I have my sketch(simplified version) as shown:
int sensorPin = 0; // select the input pin for the photocell
int ledPin = 11; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int auto = 0;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.print("Digital reading = ");
Serial.println(sensorValue); // the raw analog reading
// turn the ledPin on
if (auto > 1)
if (sensorValue < 700){
digitalWrite(ledPin, LOW);
}else{
digitalWrite(ledPin,HIGH);
}
// stop the program for <sensorValue> milliseconds:
delay(10);
}
however it shows the error shown and highlights this line. int auto = 0; I have tried everything in my power, such as moving it, and changing it to a Boolean, but i can't get it to work.
"auto" is a keyword meaning, that a variable is automatically given the data type of its initialiser. Replace it with a non-reserved work and it will compile.
This IDE does not syntax it. Where np++ and others does.

for loop Arduino/C

My aim is to create a for loop that iterates through numbers and once it reaches the maximum, it stops printing. So far I managed to create a piece of code that stops printing the x but it keeps printing zeroes. How can I stop Serial.print() function to be executed once the iteration reached the maximum value?
int x;
boolean f = false;
void setup(){
Serial.begin(9600);
}
void loop(){
for(x=0;x<8;x++){
Serial.println(x);
delay(300);
if(x==7){
f = true;
}
if(f){
break;
}
}
}
Something like below should serve. Btw, I like to name my vars something meaningful to avoid potential confusion and make the code more intelligible.
(In general you are better off posting questions to the Arduino forum. More traffic and more knowledgeable/helpful people == more likelihood of getting an answer.)
int current;
int limit;
boolean complete;
void setup(){
Serial.begin(9600);
current = 0;
limit = 8;
complete = false;
}
void loop(){
if (!complete){
while (true){
Serial.println(current);
current++;
if (current >= limit){
complete = true;
break;
}
delay(300);
}
}
}
The key is the word loop - that function is called repetitively!
If you want something to happen once, do it in setup(), or (as another answer suggests), have a flag to keep track of the fact that you've done it already.
Another way (since x is global) would be:
void loop() {
if (x < 8) {
Serial.println(x);
x++;
}
}
or, getting rid of the global variable:
void loop() {
static int x = 0;
if (x < 8) {
Serial.println(x);
x++;
}
}
int x;
boolean f = false;
void setup(){
Serial.begin(9600);
}
Very similar to your code, just put the print statement in the if statement and you're set.
void loop(){
for(x=0;x<8;x++){
if(!f) {
Serial.println(x);
}
delay(300);
if(x==7){
f = true;
}
}
}

Resources