Arduino pro mini A6/A7 as output - arduino

i,m trying to use pins A6 and A7 as digital output pins . but there is 1.5v (instead of 3V) on this pins as output high.
fp[0] = A1;
fp[1] = A2;
fp[2] = A3;
fp[3] = A6;
fp[4] = A7;
fp[5] = 6;
fp[6] = 7;
fp[7] = 8;
fp[8] = 9;
fp[9] = 10;
for (int i = 0; i < fp_size; i++) {
pinMode(fp[i], OUTPUT);
digitalWrite(fp[i], HIGH);
}

If you read the datasheet for AT328p, it indicates that A6 & A7 are analog exclusive pins. You can't use them as general purpose digital pins.

Related

Having some problems with Serial.write in Arduino

I'm learning Arduino and programming some basic stuff just to exercise, but I'm stuck with a RGB LED. I'll link the code in order for you to see what's happening.
int RGB_R = A5,
RGB_G = A4,
RGB_B = A3;
void setup() {
pinMode(RGB_R, OUTPUT);
pinMode(RGB_G, OUTPUT);
pinMode(RGB_B, OUTPUT);
Serial.begin(9600);
}
void loop() {
for(int i = 0; i < 256; i++){
Serial.write(i);
for(int j = 0; j < 256; j++){
for(int k = 0; k < 256; k++){
RGB_COLOR(i,j,k);
}
}
}
}
void RGB_COLOR(int R, int G, int B){
analogWrite(RGB_R, R);
analogWrite(RGB_G, G);
analogWrite(RGB_B, B);
}
I want to change the color of the RGB slowly as i, j and k increment, but it's changing between 2 colors. Another problem that I'm having is that I can't print in Serial what's the value of i, j and k.
Hope you guys can help me.
you have to provide some delay after setting the RGB_COLOR
because the code excuted fast and you can't see the change add a delay function it will be slower changeing
void loop() {
for(int i = 0; i < 256; i++){
for(int j = 0; j < 256; j++){
for(int k = 0; k < 256; k++){
// sending data to pc
Serial.print("\ti= ");Serial.print(i);
Serial.print("\tj= ");Serial.print(j);
Serial.print("\tk= ");Serial.println(k);
// change the data
RGB_COLOR(i,j,k);
delay(100); // wait for 100 ms
}
}
}
}
update :
you have to connect pins on PWM pin ... if you use arduino uno it will be (10,11,6,5,3) and change the connection
int RGB_R = 10,
RGB_G = 11,
RGB_B = 6;
the full code
connect the pins on (10,11,6) ... because analogWrite only work correctlly with pwm pins
int RGB_R = 10,
RGB_G = 11,
RGB_B = 6;
void setup() {
pinMode(RGB_R, OUTPUT);
pinMode(RGB_G, OUTPUT);
pinMode(RGB_B, OUTPUT);
Serial.begin(9600);
}
void loop() {
for(int i = 0; i < 256; i++){
for(int j = 0; j < 256; j++){
for(int k = 0; k < 256; k++){
// sending data to pc
Serial.print("\ti= ");Serial.print(i);
Serial.print("\tj= ");Serial.print(j);
Serial.print("\tk= ");Serial.println(k);
// change the data
RGB_COLOR(i,j,k);
delay(100); // wait for 100 ms
}
}
}
}
void RGB_COLOR(int R, int G, int B){
analogWrite(RGB_R, R);
analogWrite(RGB_G, G);
analogWrite(RGB_B, B);
}

Why is TCNT1 not counting up on Atmega328?

I have the following code for the Arduino with Atmega328 and a common 16x2 LCD. The LCD is working, but it is always showing the starting value "333" of the Timer 1 counter TCNT1. Why? I have read the datasheet of the 328 over and over again, but I don't get it.
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int lcdContrastPin = 6, lcdBackligthPin = 10;
void setup()
{
// tutn on LCD backlight and contrast
pinMode(lcdContrastPin, OUTPUT);
pinMode(lcdBackligthPin, OUTPUT);
// fine-tuning contrast could be done by PWM on lcdContrastPin
digitalWrite(lcdContrastPin, LOW);
digitalWrite(lcdBackligthPin, HIGH);
lcd.begin(16, 2);
// configure Timer1
TCCR1A = 0; // no waveform generation
TCCR1B = 0x00000010; // frequency divider 8 (i.e. counting with 2 MHz)
TCCR1C = 0;
TIFR1 = 0x00100000; // clear Input Capture Flag
TCNT1 = 333;
}
void loop()
{
int currentTimerValue = TCNT1;
lcd.setCursor(0, 0);
lcd.print("TCNT1=");
lcd.print(currentTimerValue);
lcd.println(" ");
delay(50);
}
Stupid me! In a lapse of consciousness I took 0x00000010 as a binary number instead of as a hexadecimal which it is. As a result I set the all clock selection bits to 0 which means the timer stops.
After replacing 0x00000010 by 0b00000010 (the true binary number) everything works as expected now:
TCCR1B = 0b00000010; // frequency divider 8 (i.e. counting with 2 MHz)
TCCR1C = 0;
TIFR1 = 0b00100000; // clear Input Capture Flag

using Ultrasonic sensor to find obstecles Arduino

Am trying to make a smart cane for blind ppl using 2 ultrasonic sensors
to detect obstacles, and use a buzzer and a flat vibrating motor as a feedback when an obstacle is detected, where the flat motor should be ON when an obstacle
between 1m - 3m is detected, and the buzzer when it's less than 1m.
now recently i used the NewPing library which solved some of the problems but
the code doesn't do exactly what i want, instead it triggers both the buzzer and motor together when object detected, i'd appreciate it if anyone could help.
#include <NewPing.h>
const int trigPin = 8;
const int trigPin1 = 13;
const int echoPin = 9;
const int echoPin1 = 12;
const int buzzer = 5;
const int motor = 3;
NewPing sonar1(trigPin,echoPin,maxout);
NewPing sonar2(trigPin1,echoPin1,maxout);
// defines variables
long duration;
long duration1;
int distance;
int distance1;
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(motor, OUTPUT);
Serial.begin(9600);
}
void loop() {
distance = sonar1.ping_cm();
distance1 = sonar2.ping_cm();
if (distance > 100 || distance1 > 100) {
digitalWrite(buzzer,LOW);
digitalWrite(motor,HIGH);
}
else if (distance <= 100 || distance1 <= 100) {
digitalWrite(buzzer,HIGH);
digitalWrite(motor,LOW);
}
Serial.print("Distance1: ");
Serial.println(distance);
Serial.print("Distance2: ");
Serial.println(distance1);
}
this is the ultrasonic pins:
first sensor (Vcc = 5V, trig = 8, echo = 9, GND = GND)
second sensor(Vcc = 5V, trig = 13 , echo = 12 , GND = GND)
and this is the buzzer and motor pins:
buzzer = 5 , GND
motor = 3 , GND
Separate the logic for the motor and the buzzer:
if (distance >= 100 && distance <= 300) {
// Motor on
digitalWrite(motor, HIGH);
}
else {
// Motor off
digitalWrite(motor, LOW);
}
if (distance1 < 100) {
// Buzzer on
digitalWrite(buzzer, HIGH);
}
else {
// Buzzer off
digitalWrite(buzzer, LOW);
}

Syncing Processing to an Arduino that is Multiplexing

I am sending 2 bytes from Processing to Arduino. These bytes are then stored in two values on the Arduino side, and then sent to a function that will then display them on a 7 segment 4 digit display.
The numbers are displayed fine! Yes, until about 5 seconds. I recorded it in slow motion.
9561 runs for about 5 seconds,
then a flash of 6100 very shortly,
and then 6195 very shortly
then it returns to 9561 for about 5 seconds again.
This can be changed slightly by altering the time each number is on the display, and I try to keep it below 4.
Anyways then changing the speeds on both programs also alters it, but I can't seem to get it right. Do I have to sync the programs through altering the speeds or is there a way to sync the numbers? I read into serialEvent() but not sure how to incorporate it into my project.
Here's my code below for the PROCESSING
void setup()
{
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
seg[0] = 9;
seg[1] = 5;
seg[2] = 6;
seg[3] = 1;
int x = (seg[pos+1] & 0x000f)<<4|(seg[pos]) & 0x000f; //3210
int x2 = (seg[pos+3] & 0x000f)<<4|(seg[pos+2] & 0x000f);
myPort.write(x);
myPort.write(x2);
//0123
int disp1 = x;
int disp2 = (x & 0x00f0)>>4;
int disp3 = (x2 & 0x000f);
int disp4 = (x2 & 0x00f0)>>4;
//0123
print(disp1, '\n', disp2, '\n', disp3, '\n', disp4, '\n', '\n');
}
And ARDUINO
void setup() {
pinMode(GND1, OUTPUT);
pinMode(GND2, OUTPUT);
pinMode(GND3, OUTPUT);
pinMode(GND4, OUTPUT);
pinMode(aPin, OUTPUT);
pinMode(bPin, OUTPUT);
pinMode(cPin, OUTPUT);
pinMode(dPin, OUTPUT);
pinMode(ePin, OUTPUT);
pinMode(fPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(light, OUTPUT);
Serial.begin(9600);
}
void digitdisplay(byte x,byte x2)
{
//unsigned char tn = int (num/1000);
//if (tn>10)
//{
// tn = tn - 10;
//}
//
//unsigned char hn = int (num/100)%10;
//unsigned char tenn = int (num/10)%10;
//unsigned char on = int (num%10);
int disp[4];
disp[0] = ( x & 0x000f);
disp[1] = ( x & 0x00f0)>>4;
disp[2] = ( x2 & 0x000f);
disp[3] = ( x2 & 0x00f0)>>4;
numberselect(disp[0]);
digitselect(1);
delay(t);
numberselect(disp[1]);
digitselect(2);
delay(t);
numberselect(disp[2]);
digitselect(3);
delay(t);
numberselect(disp[3]);
digitselect(4);
delay(t);
}
void loop()
{
int x,x2;
if (Serial.available()>0)
{ // If data is available to read,
x=Serial.read();
x2=Serial.read();
}
//shiftlight(maxrange);
digitdisplay(x,x2);
}

Arduino for Christmas light control - invert code

I have an arduino uno connected to a 8 channel relay board. I want to use it with Vixen 3. When I upload the code all the relays turn on. So, when i send a signal in Vixen the relay turns off. I need a way to invert this so when I send the signal from Vixen it turns on the relay. The coding side isn't really my strong point, so please go easy on me.
With love xx
int C1 = 2;
int C2 = 3;
int C3 = 4;
int C4 = 5;
int C5 = 6;
int C6 = 7;
int C7 = 8;
int C8 = 9;
int i = 0;
int incomingByte[8];
void setup()
{
Serial.begin(9600);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
pinMode(C3, OUTPUT);
pinMode(C4, OUTPUT);
pinMode(C5, OUTPUT);
pinMode(C6, OUTPUT);
pinMode(C7, OUTPUT);
pinMode(C8, OUTPUT);
}
void loop()
{
if (Serial.available() >= 8) {
for (int i=0; i<=8; i++)
{
incomingByte[i] = Serial.read();
}
analogWrite(C1, incomingByte[0]);
analogWrite(C2, incomingByte[1]);
analogWrite(C3, incomingByte[2]);
analogWrite(C4, incomingByte[3]);
analogWrite(C5, incomingByte[4]);
analogWrite(C6, incomingByte[5]);
analogWrite(C7, incomingByte[6]);
analogWrite(C8, incomingByte[7]);
}
}
Try this code:
int C1 = 2;
int C2 = 3;
int C3 = 4;
int C4 = 5;
int C5 = 6;
int C6 = 7;
int C7 = 8;
int C8 = 9;
int i = 0;
int incomingByte[8];
void setup()
{
Serial.begin(9600);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
pinMode(C3, OUTPUT);
pinMode(C4, OUTPUT);
pinMode(C5, OUTPUT);
pinMode(C6, OUTPUT);
pinMode(C7, OUTPUT);
pinMode(C8, OUTPUT);
digitalWrite(C1, LOW);
digitalWrite(C2, LOW);
digitalWrite(C3, LOW);
digitalWrite(C4, LOW);
digitalWrite(C5, LOW);
digitalWrite(C6, LOW);
digitalWrite(C7, LOW);
digitalWrite(C8, LOW);
}
void loop()
{
if (Serial.available() >= 8) {
for (int i = 0; i <= 8; i++)
{
incomingByte[i] = Serial.read();
}
analogWrite(C1, incomingByte[0]);
analogWrite(C2, incomingByte[1]);
analogWrite(C3, incomingByte[2]);
analogWrite(C4, incomingByte[3]);
analogWrite(C5, incomingByte[4]);
analogWrite(C6, incomingByte[5]);
analogWrite(C7, incomingByte[6]);
analogWrite(C8, incomingByte[7]);
}
}

Resources