Go Down

Topic: Simple code not working... (Read 296 times) previous topic - next topic

amarotica

Dec 08, 2015, 06:09 am Last Edit: Dec 08, 2015, 06:15 am by amarotica
Hello, I pieced this code together for an Attiny85 but I can't get it to work... I know I'm uploading correctly because the "blink" sketch works when I set the output pin to 0 as in PWM0 on attiny85... Any ideas? I'm trying to make a reed switch on PWM pin 1(6) turn PWM pin 0(5) to high when the reed is released and stay high. Also what I cannot figure out that would be awesome if someone knew how to do this easily is to make PWM 0 stay high for 5 seconds after the reed switch is released and then shut back down... Anyways, thanks for the help in advance!

Code: [Select]
const int buttonPin = 1;     // the number of the pushbutton pin
const int steadyvoltagepin =  0;      // the number of the steady volatage pin



int buttonState = 0;    

void setup() {
 // initialize the Steady voltage pin as an output:
 pinMode(steadyvoltagepin, OUTPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);
}

void loop() {
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);
 //Give +5V continuously to the pin.
 digitalWrite(steadyvoltagepin, HIGH);
 
 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {
   // Generate pulse of 50% DC:
   //for 50% dc of frequency 500Hz
   digitalWrite(steadyvoltagepin, LOW);
  
 } else {
   // Donot Generate the pulse or give 0V:
     digitalWrite(steadyvoltagepin, HIGH);//0V
 }
}

MorganS

While the comments in the code make it look like you understand what "PWM" means, your description and your code look like you do not. Try to explain again what you think this code should do and what it actually does. "It doesn't work" is not an acceptable problem description.

You have an output called steadyvoltagepin but you want to tun it off?

Look at the example sketch BlinkWithoutDelay to see what's possible with millis(). To keep the light lit for 5 seconds after the input goes high, you should record the time when it went high, with millis().
"The problem is in the code you didn't post."

amarotica

Sorry, I am wanting a button (PWM pin 0 to button to Ground) to turn on a led for five seconds on PWM pin 1 then shut down led. Then after that, wait until it happens again.

cmiyc

Sorry, I am wanting a button (PWM pin 0 to button to Ground) to turn on a led for five seconds on PWM pin 1 then shut down led. Then after that, wait until it happens again.
PWM (Pulse Width Modulation) is done on output.

Why would you be using PWM for a button (INPUT)?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

amarotica

It is a digital I/O pin that I assumed was ok to call PWM because on arduinos they say "Digital PWM" even though they are used for input and output, sorry for not being "grammatically correct"

cmiyc

Yeah, a regulator for a solenoid might be over kill.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Go Up