Array accessing in arduino and for loop - arduino-uno

How can I access data values from the arrays in Arduino programming?
The program is given below :
int myArraylt[24]= {3530,1580,3880,2780,4040,11260,7935,6655,2100,5100,1450,2200,2200,5900,6180,4230,2405,3560,4535,12635,12085,3500,930,3430};
int myArraygt[24]= {0,0,0,0,0,0,6320,5496.9,5948,4124.1,3848.4,3573,3022.2,3297.6,3298.2,3573,4123.2,0,0,0,0,0,0};
void setup() {
for (int i=1;i=1;i++)
if (myArraygt(i)>myArraylt(i))
println( SSystem is on MG);
else
println( SSystem is on GRID);
}
void loop() {
// put your main code here, to run repeatedly:
}

In your code there are few mistakes
array index starts from 0 ,but in your for loop you started it from 1
In the loop condition expression is checked as i=1 it runs loop only for array index 1
In order to access data values from array use array[i] format(not array(i))
for printing in arduino use Serial.println() ,not just println()
For accessing the array values the code should be as follows
int myArraylt[24]= {3530,1580,3880,2780,4040,11260,7935,6655,2100,5100,1450,2200,2200,5900,6180,4230,2405,3560,4535,12635,12085,3500,930,3430};
int myArraygt[24]= {0,0,0,0,0,0,6320,5496.9,5948,4124.1,3848.4,3573,3022.2,3297.6,3298.2,3573,4123.2,0,0,0,0,0,0};
void setup() {
for (int i=0;i<24;i++)
if (myArraygt[i]>myArraylt[i])
Serial.println( SSystem is on MG);
else
Serial.println( SSystem is on GRID);
}
void loop() {
// put your main code here, to run repeatedly:
}
And if you want to print SSystem is on MG and SSystem is on GRID on the screen then it must be put inside double quotes as Serial.println( "SSystem is on MG"); and Serial.println( "SSystem is on GRID");

Related

Pointer not printing char[] array

I'm writing some code to take in a string, turn it into a char array and then print back to the user (before passing to another function).
Currently the code works up to dat.toCharArray(DatTim,datsize); however, the pointer does not seem to be working as the wile loop never fires
String input = "Test String for Foo";
InputParse(input);
void InputParse (String dat)
//Write Data
datsize = dat.length()+1;
const char DatTim[datsize];
dat.toCharArray(DatTim,datsize);
//Debug print back
for(int i=0;i<datsize;i++)
{
Serial.write(DatTim[i]);
}
Serial.println();
//Debug pointer print back
const char *b;
b=*DatTim;
while (*b)
{
Serial.print(*b);
b++;
}
Foo(*DatTim);
I can't figure out the difference between what I have above vs the template code provided by Majenko
void PrintString(const char *str)
{
const char *p;
p = str;
while (*p)
{
Serial.print(*p);
p++;
}
}
The expression *DatTim is the same as DatTim[0], i.e. it gets the first character in the array and then assigns it to the pointer b (something the compiler should have warned you about).
Arrays naturally decays to pointers to their first element, that is DatTim is equal to &DatTim[0].
The simple solution is to simply do
const char *b = DatTim;

if statement in while loop arduino

code dose not perfect work
int pushButton = 2;
int gearstatus = 0 ;
int buttonState;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
void gearfunction(){
buttonState = digitalRead(pushButton);
while(gearstatus <= 5){
Serial.println( gearstatus);
if(buttonState == HIGH){
gearstatus++;}
}
}
void loop() {
gearfunction();
}
in this code i am trying to if statement in while loop,
but code doesn't work . can some one give me how to did this ? i want to increase gearstatus up to 5 but value not increase .
It doesn't work because marking pushButton as INPUT wont make it equal to HIGH
You need to put inside setup function after the input instruction:
digitalWrite(pushButton,HIGH)
A Button-Pin has to be initialized on Setup, like in the preceding answer or with:
pinMode(2, INPUT_PULLUP); //Pin-D2. This command activates it's internal
//resistor, so the resulting signal is clear HIGH and not floating like a duck...
Hint: on StartUp all pins of a uC are floating-inputs, so if pulled-UP
they may be grounded with a button or anything else like a sensor, NTC-Resistor, etc.), resulting in a clear "1" or "0" - or a defined Analog-Signal, which later may be scanned like:
boolean buttonState = digitalRead(2); //Pin D2
//or
int value = analogRead(A0); //Pin A0
Can you please explain a little what you are trying to do, It would be better if you attach a small image of the circuit. I can suggest you the code and answer using circuits.io if you give the required info.

Linux subsystem on windows cannot compile java code

I have installed java, followed all the ubuntu specific advises that I found on SO.However, as I do javac Program.java, it looks like I'm entering an infinite loop.Just keep typing and can't even get out(even clt-c does not work).
Reverse.java
/**
* This program echos the command-line arguments backwards.
**/
public class Reverse {
public static void main(String[] args) {
// Loop backwards through the array of arguments
for(int i = args.length-1; i >= 0; i--) {
// Loop backwards through the characters in each argument
for(int j=args[i].length()-1; j>=0; j--) {
// Print out character j of argument i.
System.out.print(args[i].charAt(j));
}
System.out.print(" "); // add a space at the end of each argument
}
System.out.println(); // and terminate the line when we're done.
}
}
I can run other programs(Ruby, C++, C etc).There's sth that I'm missing here and I will appreciate any help.

RNG giving same number after second interation in loop

So, I'm trying to make a game that requires randomly colored pictureboxes. I've been trying to make the random color generator, but I'm running into an issue that I can't explain.
When this code runs (inside of Form1_Load event):
for(int i=0; i<6, i++)
{
DateTime moment = DateTime::Now;
Random^RNG=gcnew Random(moment.Millisecond);
color[i]=RNG->Next(16);
if(color[i]<=9)
{
colorStr[i]=color[i].ToString();
}
else if(color[i]==10)
{
colorStr[i]="A";
}
else if(color[i]==11)
{
colorStr[i]="B";
}
else if(color[i]==12)
{
colorStr[i]="C";
}
else if(color[i]==13)
{
colorStr[i]="D";
}
else if(color[i]==14)
{
colorStr[i]="E";
}
else if(color[i]==15)
{
colorStr[i]="F";
}
FullColor+=colorStr[i]; //FullColor was initialized with a value of "#";
}
this->textBox1->Text=FullColor;
this->Player->BackColor = System::Drawing::ColorTranslator::FromHTML(FullColor);
The textbox displays either all the same number (i.e. #000000), or the first number will be unique, but the other five will be equal to each other (i.e. #A22222).
Random generator should not be re-created every time. Try to do it once, before the loop:
Random^RNG=gcnew Random(moment.Millisecond);
for(int i=0; i<6, i++)
{
....
(In your case, it seems that the moment.Millisecond is the same for sequential calls. But even if it would be different, the generator is not supposed to be re-created.)
Instead of the loop, you may consider the following code:
Random^ RNG = gcnew Random(); // somewhere at the beginning
....
int color = RNG->Next(0x1000000);
String^ colorStr = color.ToString("X6");

How to record the running information of functions in your program?

I recently attended a coding interview and I was asked a question which I didn't know the answer to. After searching the answer on the internet for a few day, I come here call for help.
The question is described as followed: You should propose a approach to record the running information of function in your program, for example, the times of a function called, and so on.
By the way, you are not allowed to modify these functions. Maybe you want to define a global variant in these function to record the running function, but that is not allowed.
Ok! That's all about the question I met in a coding interview.
This is the best I could come up with using C++ macros. I don't know whether it conforms to the requirements.
A very basic version just recording the count. The macro replaces all existing calls to the function with the contents of the macro, which records the stats and calls the function. Can easily be extended to record more details. Assumes there's only one function with that name or you want one count for all of them. Requires a macro for each function.
// here's our function
void func()
{ /* some stuff */ }
// this was added
int funcCount = 0;
#define func(...) do { funcCount++; func(__VA_ARGS__); } while(0)
int main()
{
// call the function
func();
// print stats
cout << funcCount << endl;
return 0;
}
Prints 1.
A more generic version. Requires changes to how the function is called.
// here are our functions
void someFunc()
{ /* some stuff */ }
void someOtherFunc()
{ /* some stuff */ }
// this was added
map<string, int> funcCounts;
#define call(func, ...) do { funcCounts[ #func ]++; func(##__VA_ARGS__); } while(0)
int main()
{
// call the functions
// needed to change these from 'someFunc();' format
call(someFunc);
call(someOtherFunc);
call(someFunc);
// print stats
for (map<string, int>::iterator i = funcCounts.begin(); i != funcCounts.end(); i++)
cout << i->first << " - " << i->second << endl;
return 0;
}
Prints:
someFunc - 2
someOtherFunc - 1

Resources