i working on home light project , have working, in simple fashion 1 switch controls multiple lights.
i have introduced array checks previous state, if there no change doesn't drop routine turns light on or off. i have 2 reasons doing this, first @ moment routine pulling pin high. the second , more import reason, have created project controls lights via webpage. i combine switches , web control 1 project. if have switches saying off , web saying on cause me problems.
with previous state array introduced switches control wrong light or not @ , seems in random state.
sorry lack of code blocks have checked code compiles on machine , copied again , included code blocks sorry causing problems , taking look. project has been cobbled , of ways c++ references arrays different me.
i have introduced array checks previous state, if there no change doesn't drop routine turns light on or off. i have 2 reasons doing this, first @ moment routine pulling pin high. the second , more import reason, have created project controls lights via webpage. i combine switches , web control 1 project. if have switches saying off , web saying on cause me problems.
with previous state array introduced switches control wrong light or not @ , seems in random state.
sorry lack of code blocks have checked code compiles on machine , copied again , included code blocks sorry causing problems , taking look. project has been cobbled , of ways c++ references arrays different me.
code: [select]
const int zone2[] = {8,13,0};
const int zone3[] = {11,0};
const int zone4[] = {7,0};
const int zone5[] = {9,0};
const int zone6[] = {12,0};
const int * zones[]={zone2,zone3,zone4,zone5,zone6};
int buttonstate[] = {0,0}; // variable reading pushbutton status
int previousstate[]={0,0,0,0,0}; // array holding previous state
void setup()
{
//initialize output pins control lights
pinmode(11, output);//need loop through arrays setting pins output
pinmode(12, output);
pinmode(13,output);
pinmode(7, output);//need loop through arrays setting pins output
pinmode(8, output);
pinmode(9,output);
// initialize pushbutton pin input:
//set light switches same block ie pins 30 - 35
byte i;
//this loop sets pins inputs 4
(i=30;i< 36;i++) {
pinmode(i, input);
digitalwrite(i,high); // makes connect internal resistor
}
}
void loop()
{
// read state of pushbutton value:
byte myinput =2;
// check if pushbutton pressed.
// if is, buttonstate high:
int arraycount;
int arrayposition;
(int z = 0; z < 5; ++z)
{
buttonstate[z] = digitalread(z+30);
for (arrayposition = 0;zones[z][arrayposition] ; arrayposition++)
{
if ((buttonstate[z] == high) && (previousstate[z] == 0 )) {
// turn led on:
digitalwrite(zones[z][arrayposition],high);
previousstate[z] = 1;//i have added line handle previous state
}
else if ((buttonstate[z] == low) && (previousstate[z] == 1)) {
// turn led off;
digitalwrite(zones[z][arrayposition],low);
previousstate[z] = 0;//i have added line handle previous state
}
}
}
}
Arduino Forum > Using Arduino > Programming Questions > Array going wrong
arduino
Comments
Post a Comment