The program for LEDs 8x8 doesn't function in Arduino Uno. I'm not using the pinMode() method. I'm using the other method for specification - c

The program for LEDs 8x8 doesn't function in Arduino Uno. I'm not using the pinMode() method to specify pin OUTPUT for LEDs. I'm using the method "LedControl"
The specification pin:
"lc=LedControl(4,6,5,1);"
for setting the pin. Why ?
This program compiles and executes without problems.
I'm importing the library correctly.
I'm using this example:
#include "LedControl.h"
/*Now we need a LedControl to work with.
pin 4 is connected to the DataIn
pin 5 is connected to the CLK
pin 6 is connected to LOAD / CS
We only have a single MAX7219 */
LedControl lc=LedControl(4,6,5,1);
/* we always wait a bit between updates of the display */
unsigned long delaytime=500;
void setup() {
/* The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call */
lc.shutdown(0,false);
lc.setIntensity(0,8);
lc.clearDisplay(0);
}
void loop() {
lc.setIntensity(0,8);
single();
lc.clearDisplay(0);
}
/* This function will light up every Led on the matrix. The led will blink along with the row-number. row number 4 (index==3) will blink 4 times etc. */
void single() {
for(int row=0;row<8;row++) {
for(int col=0;col<8;col++) {
delay(50);
lc.setLed(0,row,col,true);
delay(50);
for(int i=0;i<col;i++) {
lc.setLed(0,row,col,false);
delay(50);
lc.setLed(0,row,col,true);
delay(50);
}
}
}
}

The LedControl library uses pinMode in the background to make the LEDs easier to control. When you give values to LedControl, it automatically uses pinMode, which operates as pinMode(pin, OUTPUT|INPUT).

Related

Perform Certain task of LED blink on atmega328p

Build a modular program using two switches and two LEDs on a atmega328p. Follow the below conditions:
Switch-I Switch-II LED 1 state LED 2 state
open open LOW LOW
open close LOW HIGH
close open HIGH LOW
close close Toggle LED Toggle LED
We have also to make two functions :
GPIOConfig(Pin, mode)
Purpose: The function is used to configure the mode of the pin.
Pin: The Atmega328P port pin which need to be configured.
Mode: direction of the pin in INPUT or OUTPUT. In case of INPUT, the mode is required to be
configured for PULLUP configuration along with INPUT.
GPIOPinWrite(pin, state)
Purpose: The function is used to write LOW or HIGH state to GPIO pin.
Pin: The Atmega328P pin used to write LOW or HIGH state.
State: LOW or HIGH
Here is the code which I have written:
#include"GPIO.h"
#include<stdint.h>
#include<avr/io.h>
#include<util/delay.h>
#define SET_BIT(PORT,BIT) PORT|= (1<<BIT)
#define CLR_BIT(PORT,BIT) PORT&= ~(1<<BIT)
//#define TOGGLE_BIT(PORT,BIT) PORT^= (1<<BIT)
void GPIOConfig(uint8_t PORT,uint8_t PIN);
void GPIOPinWrite(uint8_t PIN,char* STATE);
int main()
{
GPIOConfig(DDRD,PD7);
GPIOConfig(DDRD,PD6);
GPIOConfig(DDRD,PD3);
GPIOConfig(DDRD,PD2);
//GPIOConfig();// Insert code
while(1)
{
//GPIOPinRead();
uint8_t PIN_READ=0x00; // 00000000
if(PIN_READ==PIND)
if ((PIN_READ & (1<<PD2)) && PIN_READ & (1<<PD3)) // (00000100)& (00000100) //Pooling
{
while(1)
{
GPIOPinWrite(PD7,"HIGH");
GPIOPinWrite(PD6,"HIGH");
_delay_ms(1000);
GPIOPinWrite(PD7,"LOW");
GPIOPinWrite(PD6,"LOW");
_delay_ms(1000);
}
}
else if((PIN_READ & (1<<PD2)))//&& (PIN_READ & ~(1<<PD3)))
{
GPIOPinWrite(PD7,"HIGH");
}
else if((PIN_READ & (1<<PD3)))// && (PIN_READ & (1<<PD2)))
{
GPIOPinWrite(PD6,"HIGH");
}
else
{
GPIOPinWrite(PD7,"LOW");
GPIOPinWrite(PD6,"LOW");
}
}
return 0;
}
void GPIOConfig(uint8_t PORT,uint8_t PIN)
{
if(PIN==PD6 || PIN==PD7)
{
SET_BIT(PORT,PIN);
}
else if(PIN==PD2 || PIN==PD3)
{
SET_BIT(PORT,PIN); // PD7,PD6 as output - LED is connected
CLR_BIT(PORTD,PIN); // Making it a pull up configuration
}
}
void GPIOPinWrite(uint8_t PIN,char* STATE)
{
if(STATE=="HIGH")
SET_BIT(PORTD,PIN);
else if(STATE=="LOW")
CLR_BIT(PORTD,PIN);
}
According to me the Logic is correct but the decleration and initialisation of variables and Pins are not correct that is why my LED is not blinking. Although the code does not show any error in the codeblocks.
Do check this code and please help to find out the problem and the solution to this.
This code has several fundamental problems.
The DDRD in main() is supposedly a reference to the actual register, fetched from some register map header. Then you pass the value of this register to a function accepting uint8_t PORT. The connection to the actual register is lost and your functions end up manipulating some local variable instead of the actual register.
Just forget about writing bloatware function layers, string comparisons and other irrelevant things on top of very basic stuff. Instead write everything out on a single, readable line: DDRD = PD7 | PD6 | PD3 | PD2; etc, assuming these should be outputs. Same for pull registers.
A detailed guide for how to do this without a bloatware library can be found here:
How to access a hardware register from firmware?

How to read more than one ADC input and print in C for STM32F4

I'm using the MCU STM32F4 and I want to read the ADC inputs using Hal Library and show these on the Terminal. My ADCs inputs are running in continuous convertion mode and the convertion is called by tick of a Timer.
ADC_HandleTypeDef hadc1;
TIM_HandleTypeDef htim2;
char buffer[10];
uint32_t adc1, adc2, adc3;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{
if(htim->Instance == TIM2)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1,300);
adc1 = HAL_ADC_GetValue(&hadc1);
sprintf(buffer,"A1: %d ",(int)adc1);
CDC_Transmit_FS((uint8_t*)buffer,9);
HAL_ADC_PollForConversion(&hadc1,300);
adc2 = HAL_ADC_GetValue(&hadc1);
sprintf(buffer,"A2: %d ",(int)adc2);
CDC_Transmit_FS((uint8_t*)buffer,5);
HAL_ADC_Stop(&hadc1);
if(adc1 > 1000)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,1);
}
else
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,0);
}
}
}
This code above works if I have a single channel to monitorate. But, if I use more than one channel, the output show only the channel 2 values, like the image shown below.
I dont know what is happening. Do can you help me?

Pulse generation and readout on arduino

Currently I'm working on a project where I have to read out pulses from a Arduino and check if the result is High or Low.
I had to write my own code to generate the high/low output from the Arduino:
//Pulse Generator Arduino Code
int potPin = 2; // select the input pin for the knob
int outputPin = 13; // select the pin for the output
float val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(outputPin, OUTPUT); // declare the outputPin as an OUTPUT
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin); // read the value from the k
val = val/1024;
digitalWrite(outputPin, HIGH); // sets the output HIGH
delay(val*1000);
digitalWrite(outputPin, LOW); // sets the output LOW
delay(val*1000);
}
It uses a knob to change the delay between the pulses.
Im currently trying to read the high/low data with another Arduino (Lets call this one the "count Arduino") by simply connecting the 2 with the a cable from the "outputPin" to a port on the count Arduino.
I'm using digitalRead to read the port without any delay.
//Count Arduino Code
int sensorPin = 22;
int sensorState = 0;
void setup() {
pinMode(sensorPin, INPUT);
Serial.begin(9600);
}
void loop(){
sensorState = digitalRead(sensorPin);
Serial.println(sensorState);
}
First it tried with a pulse every 1 second but the result was a spam of a ton of lows and highs. Always 3 Lows and 3 highs and repeating. It wasn’t even close to one every 1 second but more like 1 every 1 millisecond.
I cant figure out what i'm doing wrong. Is it timing issue or is there a better way to detect these changes?
a spam of a ton of lows and highs
... happens if the GND of the two Arduinos are not connected.
Also, your reading arduino prints at every loop cycle, which were a few microseconds only, if the Serial buffer would not overflow.
Better printout changes only, or use a led to show what's happening.
void loop(){
static bool oldState;
bool sensorState = digitalRead(sensorPin);
if (sensorState != oldState) {
Serial.println(sensorState);
oldState = sensorState;
}
}

how to interrupt arduino when data recieved via rx pin

what am I doing wrong ? I have the RX of the Bluetooth HC-07 wired into pin2 looking for a change in voltage when data is received to start a interrupt.
I'm trying to cause an interrupt when data is received via the Bluetooth.
#include <SoftwareSerial.h>// import the serial library
#include <PinChangeInt.h>
SoftwareSerial Genotronex(10, 11); // RX, TX
int ledpin=13; // led on D13 will show blink on / off
int BluetoothData; // the data given from Computer
void doSomething(void);
void setup() {
// wil run once
Genotronex.begin(9600);// data rate for comunicating
Genotronex.println("Bluetooth On please press 1 or 0 blink LED ..");
pinMode(ledpin,OUTPUT);
interrupts();
pinMode(2,INPUT);
attachInterrupt(0,doSomething, CHANGE);
}
void loop() {
// Main body of code
}
void doSomething(){
Genotronex.println("INTERRUPT!!!!!!!!!!!!");
digitalWrite(ledpin,1);
delay(1000);
digitalWrite(ledpin,0);
delay(1000);
detachInterrupt(0);
}
////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
This is a repost of the code ,, I've taken out what the comments said and added what you recommended,, for the most part. The code runs but change state never changes back it only changes once? I've tried taking out DetachInterrupt but the actual interrupt stops working then ? Any more ideas? Thanks for the help btw
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial Genotronex(10, 11); // RX, TX
int ledpin=13; // led on D13 will show blink on / off
int BluetoothData; // the data given from Computer
void doSomething(void);
volatile int state = LOW;
void setup() {
// wil run once
Genotronex.begin(9600);// data rate for comunicating
Genotronex.println("Bluetooth On please press 1 or 0 blink LED ..");
pinMode(ledpin,OUTPUT);
pinMode(2,INPUT);
attachInterrupt(0,doSomething, CHANGE);
}
void loop() {
// Main body of code
digitalWrite(ledpin, state);
}
void doSomething(){
Genotronex.println("INTERRUPT!!!!!!!!!!!!");
state = !state;
detachInterrupt(0);
}
delete:
detachInterrupt(0);
If you can change interrupt from CHANGE to HIGH/LOW and add something like 'debouncing'. My version here (HIGH state interrupts):
void interruptfunction() {
//your interrupt code here
while(1){
if (digitalRead(2)==LOW){
delay(50);
if (digitalRead(2)==LOW) break;
}
}
}
Or this: (not tested)
void interruptfunction() {
//your interrupt code here
if (digitalRead(2)==HIGH) state=true;
else state=false;
while(1){
if (digitalRead(2)!=state){
delay(50);
if (digitalRead(2)!=state) break;
}
}
}
If you have to react on CHANGE try leaving detachInterrupt(0); but reattach it somewhere in your main loop.

How can I create interrupts in C for Arduino?

So I did this code for Arduino in C. It is for controlling a stepper motor. But every time I have to wait until the microcontroller begins a new loop so that it can take the value for the new variables, how can I create an interrupt so it will do it in any time of the program?
#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>
#include "IO/ioconfig.h"
#include "leebotones/leebotonesA.h"
#include "leebotones/leebotonesB.h"
#include "rutina/avanza.h"
#include "rutina/retrocede.h"
char variableA = 0;
char variableB = 0;
int main(void){
ioconfig();
while(1) {
if (leebotonesA()==1) {
variableA++;
} //Fin de if leebotonesA.
if (leebotonesB()==1) {
if (variableA==0) {
variableB=1;
}
else {
variableB=0;
}
}
if (variableA==2) {
variableA=0;
PORTD=0x00;
_delay_ms(10000);
} //Fin de if variableA.
if (variableA==1 && variableB==0) {
avanza();
} //Fin de if leebotonesA.
if (variableA==1 && variableB==1) {
retrocede();
}
_delay_ms(25);
}//End of while
}// End of main
A hardware interrupt on the Arduino occurs when one of the interrupt pins receives a change of state. The function to use, if you have access to the Arduino library, is attachInterrupt.
Example code to listen for an interrupt (derived from the documentation I linked to, I added comments to help explain):
// The Arduino has an LED configured at pin 13
int pin = 13;
// Holds the current state of the LED to handle toggling
volatile int state = LOW;
void setup()
{
pinMode(pin, OUTPUT);
// First Parameter:
// 0 references the interrupt number. On the Duemilanove, interrupt 0
// corresponds to digital pin 2 and interrupt 1 corresponds to digital pin
// 3. There are only two interrupt pins for the Duemilanove and I believe
// the Uno too.
// Second Parameter:
// blink is the name of the function to call when an interrupt is detected
// Third Parameter:
// CHANGE is the event that occurs on that pin. CHANGE implies the pin
// changed values. There is also LOW, RISING, and FALLING.
attachInterrupt(0, blink, CHANGE);
}
void loop()
{
// Turns the LED on or off depending on the state
digitalWrite(pin, state);
}
void blink()
{
// Toggles the state
state = !state;
}
There is also a concept of Pin Change Interrupts that is supported on every pin. See the bottom part of introduction to interrupts for more info.
However, sometimes a hardware interrupt can be avoided by refactoring your code. For example, keep your loop() running quickly --- mostly just reading inputs, limit the use of delay() --- and in the loop, call a function when the targeted inputted value is detected.
MSTimer2 is an Arduino library function to let you set timer interrupts.
Another alternative to delay, rather than interrupts, is to set a timer and check it each time through the loop. http://arduino.cc/en/Tutorial/BlinkWithoutDelay explains the concepts. The Metro library http://arduino.cc/playground/Code/Metro implements this and is easy to use. I've used it instead of delay() so that I can check for a button push while my robot is moving.

Resources