Why does this code make an led glow red? - c

I've written this simple piece of code for testing out my arduino and RGB led.
int redled=11;
int blueled=10;
int greenled=9;
void setup()
{
pinMode(redled, OUTPUT);
pinMode(blueled, OUTPUT);
pinMode(greenled, OUTPUT);
}
void loop()
{
digitalWrite(greenled, HIGH);
digitalWrite(blueled, HIGH);
delay(1000);
digitalWrite(greenled, LOW);
digitalWrite(blueled, LOW);
delay(1000);
}
Here is a picture:
(220Ω resistors, in case you are wondering.)
As you can see, I've coded the green and blue to blink, but after I upload it onto my arduino, the red LED actually blinks. Why is this happening? How do I fix it?
Any help would be appreciated!
UPDATE:
When I unplug the red wire altogether, the program works as expected, but now, when I plug it back in, it flashes like this:
WHITE
red
WHITE
red
What is the problem now?

Your problem is that you have not turned the red LED off. Off means it needs a HIGH on the pin as you have a common anode configuration, so the LED elements go on when the voltage is LOW (not HIGH).
You are flashing the blue and green outputs, but the red pin is low all the time. So that means it is on (as the common anode is at +5V). This is because blue+green+red = white (when the blue and green outputs are LOW), whereas red alone is obviously red (when the blue and green outputs are HIGH).
So add in setup():
digitalWrite(redled, HIGH);

Related

Arduino Blinking Light not working on Breadboard

I'm just starting my Arduino 30 Days Lost in Space kit and I'm stuck on the blinking LED on the breadboard.
I have some minor experience coding. I suspect that is not the issue, but I've included it below. I have also included a picture of the breadboard. Sorry it's so grainy. Stack doesn't allow for very large files.
I got the light to blink on the HERO Board itself, but when I ran the updated code to get the LED working, BOTH stopped working.
Here is my code:
int LEDPIN = 8;
void setup() {
// put your setup code here, to run once:
pinMode(LEDPIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LEDPIN, HIGH); //High = 5V, LOW = Ground voltage
delay(500);
digitalWrite(LEDPIN, LOW);
delay(500);
}
Help would be much appreciated.

Arduino won't recognise a 5v input

I am very new to programming. I am writing a program, for an Arduino clone. I have a 12V coil relay, that works off an 12V IR switch. The relay has picked up 5V from the Arduino, and will supply 5V to pin 1, when the relay is switched EG something moving in front of the IR switch.
The program seems to ignore the incoming 5V, and continues the program as though it wasn't even trying to read it.
When I check on the serial monitor, what the pin is reading (high or low), it just shows "xxx" when there is nothing present, and doesn't react when my hand signals the switch.
I am sure it is some basic C++ code syntax error.
Code below:
void loop() {
analogWrite(channel_a_enable, 255);
//turns feeder motor on through H bridge, to feed brass case
digitalWrite(channel_a_input_1, HIGH);
digitalWrite(channel_a_input_2, LOW);
val = digitalRead (sensor_ir_pin);
//reads IR switch and checks if it has a case present yet
Serial.println(val);
if (val = HIGH)
//if brass case rolls past sensor, switching IR switch and relay
analogWrite(channel_a_enable, 0);
//turns feeder motor off
digitalWrite(channel_a_input_1, LOW);
digitalWrite(channel_a_input_2, LOW);
current_potent = analogRead(A0);
//reads the potentiometer setting (set for a max of 15 seconds time)
delay(current_potent * 14.6627);
//delays, while the brass case is in the flame, getting annealed. Also provides the multiplier for 15000 mill seconds into 1023 read point on the potentiometer

Arduino digitalRead return

I can't figure out why the returned value of digitalRead() is 0 (LOW), even with the code below.
Any idea?
void setup(){
Serial.begin(9600);
pinMode(4,INPUT);
}
void loop(){
digitalWrite(4,HIGH);
Serial.println(digitalRead(4));
}
Thanks
According to the Arduino Digital Pins documentation at http://arduino.cc/en/Tutorial/DigitalPins the digitalWrite(4, HIGH) on an input pin does not set the level of the pin. Because of the pinMode(4, INPUT), the digitalWrite(4, HIGH) turns on the processor's internal pullup resistor on pin 4.
digitalRead(4) will show HIGH or LOW, depending on what you have connected to pin 4. If you have nothing connected to pin 4, digitalRead(4) should always return HIGH because the digitalWrite(4, HIGH) connected the internal pullup resistor to it.
So the problem is in the circuit connected to the Arduino; it's not a software problem. I recommend you follow the advice of #ouah and use a multimeter, oscilloscope, or logic pen to find what the voltage is on pin 4, then debug your circuit connected to pin 4 to find out why that pin is low.
Also, I recommend you move the digitalWrite(4, HIGH) to setup() right after the pinMode(4, INPUT), because it is configuring the pin, and needs to be done only once.

ArduinoUno R3 Run Continuous routine and update even while in loop of routine

Hi everyone and thanks for looking at my post. I’m trying to get the following to occur and I’m looking for the best method by which to accomplish the task.
I have a sensor input for weight and a LED output that blinks faster as more weight is applied. I have accomplished this using a simple if else loop. See the section of code below.
lpin is my variable for the light output pin.
WeightMeasure() is my function that returns the weight in lbs.
CODE
void setup()
{
pinMode(lpin, OUTPUT);
}
void loop()
{
WeightMeasure();// get the current weight in lbs
Serial.println("The wight of the object is: ");
Serial.print(WeightMeasure);
Serial.println(" lbs");
delay(100);
//runs the if loop to turn on the LED based on weight
if(WeightMeasure > 20){
digitalWrite(lpin, LOW);
delay(50);
Serial.println("I'm more than 20 LBS”);
}
else if(WeightMeasure > 10){
digitalWrite(lpin, HIGH);
delay(800);
digitalWrite(lpin, LOW);
delay(800);
Serial.println("I'm less than 20lbs but more than 10lbs ");
}
else if(WeightMeasure > 5 ){
digitalWrite(lpin, HIGH);
delay(500);
digitalWrite(lpin, LOW);
delay(500);
Serial.println("I'm less than 10lbs but more than 5lbs");
}
else if(WeightMeasure > 3){
digitalWrite(lpin, HIGH);
delay(200);
digitalWrite(lpin, LOW);
delay(200);
Serial.println("I'm less than 5lbs but more than 3lbs");
}
else {
digitalWrite(lpin, HIGH);
Serial.println("I'm less than 3lbs");
}
END CODE
This code is meant to blink more as less weight is applied to the sensor. While this works, it has a major flaw. I have set up the delay for blinks corresponding with each range of weights in each IF ELSE statement. This does not allow for updates in real time. For example, if an item of 20lbs is applied to the sensor, it will delay sampling weight again for 800ms. I would like to achieve the appropriate time length between blinks while the weight is over 20lbs but if the weight drops from 21lbs to 19lbs rapidly, I want it to immediately break out of the 800ms wait delay and begin blinking at the appropriately faster interval. Does anyone have a thought on how best to achieve this? I’m probably doing this completely wrong but I’m using my basic understanding of programming. An example or best method and explanation would be helpful as I’m really just a beginner. If anything is unclear, please ask.
Cheers,
War
Spend some time looking at the BlinkWithoutDelay example code which is included in the Arduino examples. It uses elapsed time to control the LED blinking and doesn't lock out other code from running as delay() does.
Also maybe think about defining a function to do the blink (change the state of the LED pin) rather than repeating the same code in each if/else clause.

Arduino mega 2560 pwm timer controls

I'm trying to use a mirror galvo controlled by a mega 2560 to direct a laser. As a first test I want the laser to move along one axis by jumping equal intervals, but I'm having trouble. I'd like to know whether there are any obvious problems in my code (which I suspect may be the case as I'm new to timers and interrupts)?
void setup()
{
TCCR3B = _BV(CS30); //high freq ~50khz
pinMode(5, OUTPUT);
}
void loop() {
int output1= 5;
while(output1<255){
analogWrite(5,output1);
output1+=40;
delay(500);
}
output1=5;
}
When I run this code the mirror does achieve the full 5 degree range of movement it should. The problem is instead of moving equal intervals the mirror rotates the equivalent of .8 deg over what I suppose are the first two iterations of the while loop, then the mirror jump around 3.5deg of rotation. Then the mirror goes back to rotating by small increments again .75 deg for the last few iterations of the while loop.
I originally tried using my own settings for all timer and PWM settings, that did not work either:
void setup()
{
TCCR3A = TCCR3B = 0;
TCCR3A = _BV(COM3A1) | _BV(WGM31); //non-inverting
TCCR3B = _BV(CS30) | _BV(WGM32) | _BV(WGM33); //prescalar=1, fast pwm
ICR3= 500;
OCR3A= 6;
pinMode(5, OUTPUT);
}
void loop() {
while(OCR3A<500){
OCR3A+=80;
delay(500);
}
OCR3A=6;
}
Using this second code the mirror rotates by a constant 1 degree and stays put there.
edit: I do have a current gain circuit between the arduino and the mirrors, but I've tested both that circuit and the mirrors with a function generator. Off the function generator they work fine.

Resources