Hi all, I have been going mad trying to figure out a way to control my project, I have some experience with programming c++ but I need some guidance on what sort of control structures I should be using to do what I want.
It consists of motor that is connected to a rotor with a cam, the cam has three lobes on it and a microswitch is turned on or off by the cam.
I am using the Arduino Uno,
The Arduino IDE is version 1.0.5
I am trying to make a program that can to the following
Step1. See if the switch is open or closed (is the rotor in the right position)
-If the switch is low then turn the relay on until the switch becomes high, after becoming high the relay is switched off. (the motor-rotor is self locking so it should not move after the motor is off)
Step 2.
-Keep the relay off
-wait 5 Minutes!
THEN!!
Step 3.check the value of the photocell!
If it is above a threshold then all is great, go to step 2.
If not! then turn the relay on until the switch goes low then stop once it becomes high again.
Go to step 2
END
What I am getting instead is that the relay will turn on or off and then will not change when the two inputs change, it stays at it's one state and does not do anything different. I have fiddled around with the code and sometimes the relay will stay in one state ether on or off regardless of what the inputs are and will not change when the inputs change.
EDIT:
Here is my code that I have tried so far:
const int analogPin = A0; // pin that the photosensor is attached to
const int ledPin = 9; // pin that the relay is attached to
const int threshold = 800;
const int buttonPin = 8; switch for cam postion
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
int analogValue = analogRead(analogPin);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && analogValue > threshold) {
digitalWrite(ledPin, HIGH);
}
if (buttonState == HIGH && analogValue < threshold) {
digitalWrite(ledPin, HIGH); //low light equals low value
}
if ( buttonState == LOW && analogValue < threshold) {
digitalWrite(ledPin, LOW); //low light equals low value
}
if ( buttonState == HIGH ) {
digitalWrite(ledPin, HIGH); //low light equals low value
delay(100);
}
// print the analog value:
Serial.println(analogValue);
delay(10); // delay in between reads for stability
}