project difficulties

//pin gets high at 8 seconds while I want it at the x time

int Pin = 12, inPin1 = 2, inPin2 = 4, inPin3 = 7, inPin4 = 8;
unsigned long timer=0;
const int A=1,B=2,C=4,D=8;
int a1,a2,a3,a4,x=0;

void setup() {
Serial.begin(9600);
pinMode(Pin, OUTPUT);
pinMode(inPin1, INPUT);
pinMode(inPin2, INPUT);
pinMode(inPin3, INPUT);
pinMode(inPin4, INPUT);
digitalWrite(Pin, LOW);
}

void loop(){

timer=millis();
a1=digitalRead(inPin1);
a2=digitalRead(inPin2);
a3=digitalRead(inPin3);
a4=digitalRead(inPin4);

if (a1 = HIGH) x=+A;

if(a2 = HIGH) x=+B;

if(a3 = HIGH) x=+C;

if(a4 = HIGH) x=+D;

x=x*1000;

if (timer>=x)

digitalWrite(Pin, HIGH);

Serial.println(timer);
delay(1000);

}

 if (a1 = HIGH) x=+A;

It is unusual to assign a value to a variable in an if statement. Usually, you use == to compare a variable's content to a value.

x += 3; // adds 3 to x

thanks

Just wondering how are you with browser bookmarks?
Reason is that my Arduino references are all folder-organized bookmarks to mostly Arduino site pages.
When I code, I have a browser up with tabs to all the references I need to be sure of what I read and write.

Some basic basics.

Ideas and goals.

Arduino Libraries reference.

Details on the boards including pin maps, enough for intermediate level projects.

Beginner-Intermediate level Arduino C language reference. Maybe the page you need most now.

The tutorials.

The Arduino Playground, repository of tons of code and wiring.
http://playground.arduino.cc/Main/GeneralCodeLibrary

The base C Standard Libraries used by Arduino to program AVR chips (most Arduinos).
http://www.nongnu.org/avr-libc/user-manual/modules.html

int Pin = 12, inPin1 = 2, inPin2 = 4, inPin3 = 7, inPin4 = 8;
unsigned long timer=millis();
unsigned long timercopy;
const int A=1,B=2,C=4,D=8;
int a1,a2,a3,a4,x=0;

void setup() {
	Serial.begin(9600);
	pinMode(Pin, OUTPUT);
	pinMode(inPin1, INPUT);
	pinMode(inPin2, INPUT);
	pinMode(inPin3, INPUT);
	pinMode(inPin4, INPUT);
	digitalWrite(Pin, LOW);
}

void loop(){
	
	timercopy=millis();
	a1=digitalRead(inPin1); 
	a2=digitalRead(inPin2);
	a3=digitalRead(inPin3);
	a4=digitalRead(inPin4);
	x=0;
	if (a1 == HIGH) x+=A;	
		if(a2 == HIGH) x+=B;	
			if(a3 == HIGH) x+=C;	
				if(a4 == HIGH) x+=D;
	x=x*1000;
	if (millis()>=x+timer)
	{
		timer=millis();	
		digitalWrite(Pin, HIGH);
		Serial.println(timer);
	}
	
}

Once the output pin is HIGH it is not reset - is this what you intended?

G

what is timercopy needed for ?

Zardof:

	if (millis()>=x+timer)

That expression will let you down when x+timer rolls over in almost 50 days.
As long as you never exceed that time limit, the bug will not happen.

What always works is subtracting start time from end time to get the difference. Always Works.

Assigning powers of 2 to A,B,C,D actually obfuscates the code. It reads much more clearly with numerical constants, and the variables have no use other than to replace them. A step in the wrong direction.

Santapost
I used timercopy to check if the code worked. You can delete as not required.

GoForSmoke
You are right, I meant to subtract the value. The code raises the output pin high after a maximum of 15 seconds and does no more, so 50 days will not be a problem.

Aarg
Agree with you entirely. I was just working with Santapost code, although I did take the delay out. My intention was to get Santapost over a hurdle, not to redesign the code.

G

Zardof:
I was just working with Santapost code, although I did take the delay out. My intention was to get Santapost over a hurdle, not to redesign the code.

G

You're alright, at least you gave the OP some code help.

Here's the thing with the ornaments trimmed off it. No fancy improvements, just trying to make the code say what it does...

// santapost's code de-obfuscated

int Pin = 12, inPin1 = 2, inPin2 = 4, inPin3 = 7, inPin4 = 8;
unsigned long timer = millis();
unsigned long timercopy;
int x;

void setup() {
  Serial.begin(9600);
  pinMode(Pin, OUTPUT);
  pinMode(inPin1, INPUT);
  pinMode(inPin2, INPUT);
  pinMode(inPin3, INPUT);
  pinMode(inPin4, INPUT);
  digitalWrite(Pin, LOW);
}

void loop() {

  if (digitalRead(inPin1) == HIGH)
    x = 1000;
  else
    x = 0;
  if (digitalRead(inPin2) == HIGH) x += 2000;
  if (digitalRead(inPin3) == HIGH) x += 4000;
  if (digitalRead(inPin4) == HIGH) x += 8000;

  if (millis() - timer >= x)
  {
    timer = millis();
    digitalWrite(Pin, HIGH);
    Serial.println(timer);
  }

}

A little extra for santapost....

Those unsigned longs for time are what's taught but really any unsigned integer variable can be used for timing because of rollover (that always works). The smaller the unsigned variable used, the shorter maximum it can time to is all.

With byte (8 bit unsigned) you can time up to 255 millis (or micros). I used them for debouncing where I only have to count to 2 (or a few depending on how dirty the contact switch is). If you want 1/4 second or less timers, byte is right.

With word (16 bit unsigned) you can time up to 65.535 seconds in millis. If it takes a minute or less, 16-bit time variables will do.

With unsigned lon long (64 bit unsigned) you can milli time to billions of years.

The shorter the variable, the less RAM it uses and the faster it runs.

byte myTime;

myTime = ( millis() & 255 ); // 8 bit time, for 16 bit use & 65535

@GoforSmoke, if you're really going for efficiency, you could make an "altMillis()" that only produces 8 or 16 bit values. You know it would be faster.