Can anyone direct me as to what topic/reference I need to research for the following...
I have my pwm output running to a mosfet driver, everything works well but I am trying to set the pin high for a pre-determined amount of time, and then go into pwm for the rest of the time. But everytime through the loop it sets it back to high, then to pwm.
How do I set it high, then pwm until I tell it otherwise? I'm sure it's really simple but I don't know where to start.
Welcome to the Forum. Please read the two posts by Nick Gammon at the top of this Forum for guidelines on posting here, especially the use of code tags when posting source code...and that's the next step. We cannot be of much help unless we can see what you've done so far. Take your code and reformat it using Ctrl-T in the IDE and then post it here using code tags (see the "</>" above).
What is going to tell the Arduino that the pin should go back to a PWM value ? By the way, outputting a value of 255 using analogWrite() will make the pin go HIGH.
The thing about the loop function is that it gets called over and over and over again by main(). Your loop says to write the pin HIGH and then analog for half a second each. That's just going to keep repeating.
Under what conditions do you want which behavior? When do you want half a second of HIGH before the analogWrite and when do you want just half a second of PWM? Can you explain better what you want to see happen.
I have one more question. In the below code, is it necessary to digitalWrite the motor pins low in the while statements? Or will that be taken care of in the loop?
This is essentially an H-Bridge with a caveat. motorpin is one direction, motor2pin is the other.
int switchPin = 8;
int motorPin = 11;
int motorLevel = 110;
int motor2Pin = 10;
int motor2Level = 50;
void setup()
{
pinMode(switchPin, INPUT);
pinMode(motorPin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
}
void loop()
{
if (digitalRead(switchPin) == HIGH)
{
digitalWrite(motor2Pin, LOW);
delay(1)
digitalWrite(motorPin, HIGH);
delay(200);
analogWrite(motorPin, motorLevel);
}
else
{
digitalWrite(motorPin, LOW);
delay(1);
digitalWrite(motor2Pin, HIGH);
}
while (digitalRead(switchPin) == HIGH)
{
digitalWrite(motor2Pin, LOW);
analogWrite(motorPin, motorLevel);
}
while (digitalRead(switchPin) == LOW)
{
digitalWrite(motorPin, LOW);
analogWrite(motor2Pin, motor2Level);
}
}
So, you don't want the signal high for 200ms when the switch is HIGH, you want it to do this when the switch CHANGES from LOW to HIGH.
To make this happen, you have to "remember" what the switch was when the loop got executed last time.
int lastStateOfTheSwitch = LOW;
loop() {
int currentSateOfTheSwitch = digitalread(whatrever);
if(lastStateOfTheSwitch == LOW && currentSateOfTheSwitch ==HIGH) {
// do a 200 ms hight thing
}
if(currentSateOfTheSwitch ==HIGH) {
send current state of the motor to PWM
}
else {
send 0 to PWM
}
lastStateOfTheSwitch = currentSateOfTheSwitch;
}