Hard time trying to solve this, help please.Im posting my code with comments on my problems, which should make them easier to understand. Attached schematic of my setup.
/*
I need to control my motor soon PowerON it goes to Compressor Limit switch and stops
Stay here for a certain time, after which it moves to another switch and stops.
After another interval, the cycle repeats.
*/
int Motor = 0;
int CompressorHome = 1; // Compressor Contacts
int HeaterHome = 2;
int TimerSwitch = 3;
int TimerLED = 4;
int CompressorState;
int HeaterState;
void setup()
{
pinMode (0, OUTPUT);
pinMode (1, INPUT);
pinMode (2, INPUT);
pinMode (3, INPUT);
pinMode (4, OUTPUT);
}
void loop()
{
CompressorState = digitalRead ( CompressorHome );
if ( CompressorState == HIGH )
{
digitalWrite(Motor, HIGH); // go to CompressorHome and stop Motor
}
delay (10000); // so far, so good, motor starts, stops at CompressorHome
/* first try
if ( HeaterState = LOW )
{
digitalWrite(Motor, HIGH); // go to HeaterHome and stop Motor
}
delay (10000);
// motor stops at Compressor, doesnt restart
*/
/* next try
HeaterState = digitalRead ( HeaterHome );
if ( HeaterState = LOW )
{
digitalWrite(Motor, HIGH); // go to HeaterHome and stop Motor
}
delay (10000);
// motor doesnt restart, Im lost
*/
// now I tried with this, manually moved the shaft off home position
//reapplied power; 10s later LED coes ON, another 10s motor moves to home.
// LED remains ON, motor doesnt move.
digitalWrite ( TimerLED,HIGH);
delay (10000);
// LED goes ON, shouldnt motor start for 1s?
digitalWrite (Motor, HIGH);
delay (1000);
}
You initialize your variables to label pins, you should use them for pinMode so you don't confuse yourself when you change the wiring. So-
pinMode(Motor, OUTPUT);
You check HeaterState before it is ever given a value. In the beginning when you initialize
int HeaterState;
you can just start it off with LOW
int HeaterState = 0;
I should have said that code is for a tiny45. I know these are Rx and Tx ona 328.
Now that you point that out, question:
if I program a 328 using these pins as INPUTs or OUTPUTs, can I reprogram the chip?
You initialize your variables to label pins, you should use them for pinMode so you don't confuse yourself when you change the wiring. So-
pinMode(Motor, OUTPUT);
I usually do as you suggest
You check HeaterState before it is ever given a value. In the beginning when you initialize
int HeaterState;
you can just start it off with LOW
int HeaterState = 0;
Delta_G:
That HeaterState variable is global. So it's automatically initialized to 0 == LOW
Wont forget; now if the pin state changes, would the variable also? or do I have to with a statement?
THANKs to ALL; after tryinf a few changes I got it running though I have a few doubts. Im posting my new code with appropiate comments.
int Motor = 13;
int CompressorHome = 12; // Compressor Contacts
int HeaterHome = 11; // Heater Contacts
int TimerLED = 10; // indicates Timer code is running
int TimerSwitch = 9; // selects Timer code
void setup()
{
pinMode (Motor, OUTPUT);
pinMode (CompressorHome, INPUT);
pinMode (HeaterHome, INPUT);
pinMode (TimerSwitch, INPUT);
pinMode (TimerLED, INPUT);
}
void advance() // Motor spins too fast at 5vdc,
// so I use pulses, for better control
{
digitalWrite(13, HIGH);
delay(25);
digitalWrite(13, LOW);
delay(500);
}
void loop()
{
advance();
if (digitalRead (CompressorHome) == LOW)
{
digitalWrite (Motor, LOW); // stops at CompressorHome
delay (10000); // waits 20s
digitalWrite (Motor, HIGH); // heads for HeaterHome
delay (1000); // without this delay, motor starts but immediately goes OFF
// OK, motor starts, Arduino checks next if "loop?", HeaterHome is still HIGH
// shouldnt it loop and execute "advance();"?
}
alah:
now if the pin state changes, would the variable also? or do I have to with a statement?
No. Variables only change when you make an assignment to them. To update the value in the variable with the state of the pin you have to read the pin and assign to the variable.