Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE
What should happen is, that the PIR detects a movement and the motor should turn.
But, in my simulation the motor does not turn on.
9 Volt battery should be more than enough to get both running.
Please see here the code:
`// constants won't change. They're used here to set pin numbers:
const int PIRpin = 2;
//const int LEDpin =1; //digispark
const int LEDpin =13; //UNO
const int limitPin = 7; // the number of the pushbutton pin
//const int motorFWD = 0; //digispark
// const int motorREV = 4; //digispark
const int motorFWD = 5; //UNO
const int motorREV = 6; //UNO
const int frightDelay = 10000; //delay between actions
const int acceleration = 25; //how fast the motor starts to int acceleration delay in ms
// variables will change:
int limitState = 1; // variable for reading the pushbutton status
int PIRstate = 0;
int MOTdirection = 1;
int PWMValue = 200; // starting PWM value
void setup() { // initialize the LED pin as an output: pinMode(LEDpin, OUTPUT); pinMode(PIRpin, INPUT); pinMode(motorFWD, OUTPUT); pinMode(motorREV, OUTPUT); pinMode(limitPin, INPUT_PULLUP);
}
void loop() {
PIRstate = digitalRead(PIRpin);
if (PIRstate == HIGH ) { PWMValue=200; digitalWrite(LEDpin, HIGH); do { limitState = digitalRead(limitPin); driveMotor(); } while (limitState != 0); //state is 1 button is not activated
if (MOTdirection ==0){MOTdirection = 1;} else { MOTdirection = 0;}
stopMotor();
delay(500);
lockMotor();
delay(1000);
stopMotor();
delay(frightDelay); } digitalWrite(LEDpin, LOW);
}
void driveMotor(){
if (PWMValue < 255){++PWMValue;} delay(acceleration);
if (MOTdirection ==1)
{ analogWrite(motorFWD, PWMValue);
analogWrite(motorREV, 0);
}
else
{
analogWrite(motorFWD, 0);
analogWrite(motorREV, PWMValue);
}
}
void stopMotor(){
analogWrite(motorFWD, 0);
analogWrite(motorREV, 0);
}
void lockMotor(){
digitalWrite(motorFWD, 1);
digitalWrite(motorREV, 1);
}`
// constants won't change. They're used here to set pin numbers:
const int PIRpin = 2;
//const int LEDpin =1; //digispark
const int LEDpin =13; //UNO
const int limitPin = 7; // the number of the pushbutton pin
//const int motorFWD = 0; //digispark
// const int motorREV = 4; //digispark
const int motorFWD = 5; //UNO
const int motorREV = 6; //UNO
const int frightDelay = 10000; //delay between actions
const int acceleration = 25; //how fast the motor starts to int acceleration delay in ms
// variables will change:
int limitState = 1; // variable for reading the pushbutton status
int PIRstate = 0;
int MOTdirection = 1;
int PWMValue = 200; // starting PWM value
void setup() { // initialize the LED pin as an output: pinMode(LEDpin, OUTPUT); pinMode(PIRpin, INPUT); pinMode(motorFWD, OUTPUT); pinMode(motorREV, OUTPUT); pinMode(limitPin, INPUT_PULLUP);
}
void loop() {
PIRstate = digitalRead(PIRpin);
if (PIRstate == HIGH ) { PWMValue=200; digitalWrite(LEDpin, HIGH); do { limitState = digitalRead(limitPin); driveMotor(); } while (limitState != 0); //state is 1 button is not activated
if (MOTdirection ==0){MOTdirection = 1;} else { MOTdirection = 0;}
stopMotor();
delay(500);
lockMotor();
delay(1000);
stopMotor();
delay(frightDelay); } digitalWrite(LEDpin, LOW);
}
void driveMotor(){
if (PWMValue < 255){++PWMValue;} delay(acceleration);
if (MOTdirection ==1)
{ analogWrite(motorFWD, PWMValue);
analogWrite(motorREV, 0);
}
else
{
analogWrite(motorFWD, 0);
analogWrite(motorREV, PWMValue);
}
}
void stopMotor(){
analogWrite(motorFWD, 0);
analogWrite(motorREV, 0);
}
void lockMotor(){
digitalWrite(motorFWD, 1);
digitalWrite(motorREV, 1);
}
hi @discoverer81. Almost. See how your code is all jamed over to the side? This can and should be fixed... you can use the Autoformat tool in the IDE.
Until the code comes off your fingers naturally anf you are using tab characters to do the formatting yourself.
Many ppl just use Autoformat from time to time to fix the format even if it is already correct or just close.
It occurs to me that you may be developing your code in an environment that does not have Autoformat - I have assuned you are using the desktop Arduino IDE.
I click-copied the code from the code window in your post, pasted it into the IDE, applied the AutoFormat tool, used the "Copy for Forum" tool and came back and simply pasted that. Since it only took 29 seconds, a solid I have done for you of no consequence:
// constants won't change. They're used here to set pin numbers:
const int PIRpin = 2;
//const int LEDpin =1; //digispark
const int LEDpin = 13; //UNO
const int limitPin = 7; // the number of the pushbutton pin
//const int motorFWD = 0; //digispark
// const int motorREV = 4; //digispark
const int motorFWD = 5; //UNO
const int motorREV = 6; //UNO
const int frightDelay = 10000; //delay between actions
const int acceleration = 25; //how fast the motor starts to int acceleration delay in ms
// variables will change:
int limitState = 1; // variable for reading the pushbutton status
int PIRstate = 0;
int MOTdirection = 1;
int PWMValue = 200; // starting PWM value
void setup() { // initialize the LED pin as an output: pinMode(LEDpin, OUTPUT); pinMode(PIRpin, INPUT); pinMode(motorFWD, OUTPUT); pinMode(motorREV, OUTPUT); pinMode(limitPin, INPUT_PULLUP);
}
void loop() {
PIRstate = digitalRead(PIRpin);
if (PIRstate == HIGH ) {
PWMValue = 200; digitalWrite(LEDpin, HIGH); do {
limitState = digitalRead(limitPin);
driveMotor();
} while (limitState != 0); //state is 1 button is not activated
if (MOTdirection == 0) {
MOTdirection = 1;
} else {
MOTdirection = 0;
}
stopMotor();
delay(500);
lockMotor();
delay(1000);
stopMotor();
delay(frightDelay);
} digitalWrite(LEDpin, LOW);
}
void driveMotor() {
if (PWMValue < 255) {
++PWMValue;
} delay(acceleration);
if (MOTdirection == 1)
{ analogWrite(motorFWD, PWMValue);
analogWrite(motorREV, 0);
}
else
{
analogWrite(motorFWD, 0);
analogWrite(motorREV, PWMValue);
}
}
void stopMotor() {
analogWrite(motorFWD, 0);
analogWrite(motorREV, 0);
}
void lockMotor() {
digitalWrite(motorFWD, 1);
digitalWrite(motorREV, 1);
}
See? Now that is something programmers will have an easier time analysing on your behalf.
Which I would do now, except I'm soon away from the machines...
BTW I am sry that a long time went by. TBH I forgot all aout you, as will happen. Notice that you got a "notification", that is becuase I responded directly to your post. It would also have happened here because I referred to you @discoverer81 by using the @ symbol and your alias.
So do that. For me it helps as a reminder. Just @alto777 and while I cannot promise anything, at least that will raise a little flag in my browser window. Then if I am ignoring you it is willful, not accidental.
I try to look at the code L8R. I can quickly ask if you have used any example sketches to see if the component parts in your project function on their own as intneded: find example sketches that work with just one component and become familiar with how (and if!) the component operates and is interfaced and programmed.
Thank you for the information. I definitely did not understand all of it (or better how to to it) but i will try to follow.
I tried to get this code via single parts and they worked, so i'm pretty sure that i the whole code should work.This is my first time programming an arduino. Normally i'm working on raspberry, but with all the updates i do not know anymore which version i need to use (apt sudo install python3 (or python, or whatever).
Anyway, i think my installation went wrong. Thats why i included the Link to Tinkercad and made it public. Maybe someone can check more on this.
I did everything from start and it works...until the part with the motor controller comes.
Wish Tinkercad had also my motor driver MX1508 as part. I really think that i mix something up with the polulu motor controller.