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.
Related
I have to authenticate a transaction, say a system which will use a pre-fed authentication id to verify any user using the system. The authentication id is supposed to be changed by a super-user using communication through Serial Protocol. Each time a transaction gets completed user has to press a push button to officially finish the transaction and enable super user to feed another authentication ID.
I am able to change the authentication id using Serial event interupt in Arduino, but my pin change interupt is working only once, so I cannot finish the 2nd transaction.
I tried it without using pin change interupt also but, that made a lot of mess in my code and did not work properly as i wanted, maybe some-thing or some logic I am not able to apply correctly.
```Arduino C language`````
void setup()
{
pinMode(44, OUTPUT);
pinMode(45, OUTPUT);
pinMode(46, OUTPUT);
Serial.begin(9600);
//gsm_port.begin(9600);
// Turn on the transmission, reception, and Receive interrupt
Serial1.begin(9600);
attachInterrupt(0, pin_ISR, RISING); //0 here defines pin 2 of Mega2560
}
void pin_ISR() //ISR for when box is manually closed a latch gets closed and high value is recvd on pin 2(only pins 2,3 are GPIO interupt pin of Mega2560)//
{
b1 = digitalRead(2);
if(b1==HIGH)
{
digitalWrite(44, LOW);
digitalWrite(45, LOW);
digitalWrite(46, LOW);
memset(&fed_id[0], 1, sizeof(fed_id)); //clearing fed_id so that once used cannot be used again till new id is feeded through serial event
}
}
void serialEvent1() //Serial Rx ISR for feeding new fed_id
{
while (Serial1.available())
{
rec = Serial1.read();
a[i] = rec;
i++
}
}
void loop()
{
char key = keypad.getKey();
if (key)
{
///.....some operation here......///
switch(key)
{ //try implementing shelf not oprning feature if occupied here with each case using 3 IR sensors.
case '1': digitalWrite(44, HIGH);
break;
case '2': digitalWrite(45, HIGH);
break;
case '3': digitalWrite(46, HIGH);
break;
}
}
}//closing for if(key)
}//closing for void loop()
If the above is possible without using interupts then too i would love to have a soultion. Please help me to understand why this is not going the right way and also help me finding a solution
There was no problem with the code actually and what was discussed in comments that we need to set and reset the interrupt flags/pins, is not the case with Arduino, Arduino does it automatically, while in many controllers we do need to do those things, however the problem here was with the hardware and not any part of code. Just a resistor was creating this problem on Proteus Simulation, however on actual hardware it worked completely fine.
I am working on a project in which I want to calculate the speed of a toy car using an ultrasonic sensor by measuring the distance and once it reaches the end. We see the time that has passed from when the programme has started. However I have discovered that the sensor I am using is accurate only till 20cm. Any suggestions?
#include <NewPing.h>
int i=0;
float distance;
long duration;
int trigpin = 9;
int echopin = 10;
int s;
boolean warrant=true;
unsigned long time;
void setup() {
// put your setup code here, to run once:
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
// put your main code here, to run repeatedly:
time=millis();
if(warrant==true){
s=distance;
digitalWrite(trigpin, LOW);
delay(2);
digitalWrite(trigpin, HIGH);
delay(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = duration * 0.034 / 2;
if((distance-s)>1){
Serial.println(distance);
}
delay(200);
if(distance>30){
Serial.println(distance/time);
Serial.println("done");
delay(10);
warrant=false;
}}}
Ideally it should measure the speed easily. The code is working perfectly but it breaks down after 20cm. Probably due to the sensor's limitations
If the sensor is accurate only until 20cm and you need to measure bigger, you should have to change the sensor for a better one. The is no code that can make your sensor better.
I'm working with an Arduino for the first time in my life for a school project.
I want to make a coffee reminder alarm, so you don't forget you made a cup of coffee for yourself.
I'm working with an FSR sensor which measures the presence of the coffee cup. When the cup is placed the LED should turn on to show you a timer has started ending with a reminding buzzer.
Right now I'm using a delay, but this isn't working as I want to. Because of the delay you can remove the cup and the timer still goes on with the buzzer at the end.
I want the LED and the timer/buzzer to go off when the cup gets removed midway the process.
I never ever programmed before in my life, and I find it quite hard to get my head around it.
Here's my code:
int fsrPin = 0;
int fsrReading;
int LEDpin = 11;
const int buzzer = 7;
void setup(void) {
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop(void) {
fsrReading = analogRead(0);
Serial.print("Analog reading = ");
Serial.print(fsrReading);
if (fsrReading < 10) {
Serial.println(" - No Cup");
digitalWrite(LEDpin, LOW);
noTone(buzzer);
}
else if (fsrReading > 10) {
Serial.println(" - Cup pressent");
digitalWrite(LEDpin, HIGH);
noTone(buzzer);
delay(10000);
tone(buzzer, 700);
delay(1000);
}
delay(500);
}
The Arduino library has a millis() and a micros() functions that you should be able to make use of. They report the number of milli-seconds or micro-seconds since boot.
You can set a variable to record what time one thing happened, then later see if the the current millis() is more than say 30,000 (30 seconds) after the previous recorded time.
Adafruit has a nice tutorial on this subject, which can be found here.
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);
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.