Ok so you guys were right on the wiring for my transistor having the resistor to the base worked so its working to a point.
The momentary button is supposed to change the pwm value integer which it does but the button itself turns the dc motor on at the different pwm values.
what i wanted it to do is change the pwm values and then use the tilt switch to analog write at the desired value.
if that sounds weird and dont understand it just let me know here is my newest updated code.
also the tilt switch is wired perfectly because it works when i switch the cable from the button to it.
its got to be code
const int inPin = 3; // tilt switch or button to trigger dc motor
const int outPin = 1; // dc motor
const int buttonPin = 2; //button to change pwm
const int ledPin = 0; // led to know the state of pwm
int pwmvalue = 0;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int tiltPushCounter = 0;
int tiltState = 0;
int lasttiltState = 0;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
digitalWrite(ledPin, LOW);
digitalWrite(outPin, LOW);
}
void loop()
{
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter ++;
} else {
// if the current state is LOW then the button
// wend from on to off:
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
if (buttonPushCounter % 0 == 0)
{
digitalWrite(outPin, 0);
}
if (buttonPushCounter % 1 == 0)
{
pwmvalue = 255;
}
if (buttonPushCounter % 2 == 0)
{
pwmvalue = 180;
}
if (buttonPushCounter % 3 == 0)
{
pwmvalue = 50;
}
if (buttonPushCounter % 4 == 0)
{
pwmvalue = 70;
}
// this is where the led if button stuff was
if (buttonPushCounter >= 4)
{
buttonPushCounter = 0;
}
// read the pushbutton input pin:
tiltState = digitalRead(inPin);
// compare the buttonState to its previous state
if (tiltState != lasttiltState) {
// if the state has changed, increment the counter
if (tiltState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
tiltPushCounter ++;
}
else {
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lasttiltState = tiltState;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (tiltPushCounter % 1 == 0) {
analogWrite(outPin, pwmvalue);
}
if (tiltPushCounter >= 1) {
tiltPushCounter = 0;
}
}
please help Ive tried really hard to get this working and i appreciate all the help i've gotten thus far!