How can i fix 'case 1' in arduino - arduino

This is code
#include <SoftwareSerial.h>
int rxPin=2; int txPin=3;
SoftwareSerial BTSerial(rxPin,txPin);
int ledPin[4]={6, 9, 10, 11};
int k, num;
byte phoneData;
void setup(){
Serial.begin(9600);
BTSerial.begin(9600);
for(k=0;k<4;k++){
pinMode(ledPin[k], OUTPUT);
}}
void loop(){
if(BTSerial.available()>0){
phoneData = BTSerial.read();
num=phoneData-1;
switch(phoneData){
case 1:
while(phoneData == 1){
int val1 = analogRead(A2);
int val2 = map(val1, 0, 1023, 0, 255);
analogWrite(6,val2);
analogWrite(9,val2);
analogWrite(10,val2);
analogWrite(11,val2);
Serial.println(val2);
delay(20);}
return;
break;
case 5:
if(phoneData == 5){
for(k=0; k<4; k++){
digitalWrite(ledPin[k], LOW);}}
break;
}}}
Case 1 is working but can't break and case 5 doesn't work
Why this does not work how can i fix?
I want to case 1 is turn on LED use phone and control brightness use Potentiometer and
case 5 just turn off LED

Your switch works on phoneData already, so it's pointless to check again inside the case.
The reason why your code doesn't work is that once you enter case 1, the while loop never ends. To fix your code, do the following:
Remove the while (phoneData == 1) { loop in case 1:.
Remove the redundant return in case 1:.
Remove the if (phoneData == 5) { condition in case 5:.

Related

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

Arduino Error: 'val' does not name a type

Some students from my school are working on a project to measure vibrations (eg. earthquakes) and print the results from two sensors (Piezo knock and vibration sensor) to lcd shield. They probably compiled some codes from the internet. Their original code was like this:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int referansDegeri = 800;
int knockSensor = 0;
int val = 0;
int sensorLevel = map;
int statePin = LOW;
int THRESHOLD = 100;
const int analogPin = A0;
int vib_pin=7;
void setup()
{
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int sensorLevel = map(sensorReading, 0, 1023, 0, sensorCount);
pinMode(vib_pin,INPUT);
Serial.begin(9600);
}
void loop()
{
int sensorDegeri = analogRead(A0);
Serial.print(sensorDegeri); //Okuduğumuz değer ekrana yazdırılıyor
if (sensorDegeri >= referansDegeri){
Serial.println("siddetli sarsinti");
}
else{
Serial.println("dusuk sarsinti");
}
delay(1);
}
val = analogRead(knockSensor);
if (val >= THRESHOLD) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
Serial.println("Knock!");
delay(100);
int val;
val=digitalRead(vib_pin);
if(val==1)
{
digitalWrite(led_pin,HIGH);
delay(1000);
digitalWrite(led_pin,LOW);
delay(1000);
}
else
digitalWrite(led_pin,LOW);
}
This code gave this error (exit status):
'sensorCount' was not declared in this scope
Knowing that the students have one knock sensor, I changed the sensorCount to 1 while mapping it so it would seem
int sensorLevel = map(sensorReading, 0, 1023, 0, 1);
then it started giving this error (exit status):
'val' does not name a type
I made some search however, could not find a specific result so cannot fix the problem. But I believe it is important to mention that students were using LED Bar Graph previously, alongside LCD shield, but they have now removed it.

Why is this not controlling my shift registers?

this has been cooking my brain for hours. I just don't get what I'm doing wrong. The code seems to be wrong when entering the "if (seperate == ..)" loop. So I want to read from serial, apply the value to the first register, then read and apply to the second register, then read and apply to the third register and finally "reset" the counter(and start over).
#define data_r 2 //data input red register
#define data_g 3 //data input green register
#define data_b 4 //data input blue register
#define clock_r 6 //clock input red register
#define clock_g 7 //clock input green register
#define clock_b 8 //clock input blue register
#define clock 9 //clock pin for all three registers
#define reset 5
int seperate;
int val;
void setup() {
// put your setup code here, to run once:
pinMode(clock, OUTPUT);
pinMode(clock_r, OUTPUT);
pinMode(clock_g, OUTPUT);
pinMode(clock_b, OUTPUT);
pinMode(data_r , OUTPUT);
pinMode(data_g , OUTPUT);
pinMode(data_b , OUTPUT);
pinMode(reset, OUTPUT);
digitalWrite(reset, LOW);
delay(50);
digitalWrite(reset, HIGH);
Serial.begin(9600);
while (!Serial);
Serial.setTimeout(10);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()) {
int seperate_mod = seperate % 3;
Serial.print("seperate_mod = ");
Serial.println(seperate_mod);
if (seperate_mod == 0) {
val = Serial.parseInt();
shiftOut(data_r, clock_r, MSBFIRST, val);
} else if (seperate_mod == 1) {
val = Serial.parseInt();
shiftOut(data_g, clock_g, MSBFIRST, val);
} else if (seperate_mod == 2) {
val = Serial.parseInt();
shiftOut(data_b, clock_b, MSBFIRST, val);
}
seperate += 1;
}
}
But even more strange is that THIS CODE does work, it's only using one register:
#define data 2
#define clock 6
#define reset 5
void setup()
{
pinMode(clock, OUTPUT); // make the clock pin an output
pinMode(data , OUTPUT); // make the data pin an output
pinMode(reset, OUTPUT);
randomSeed(analogRead(0));
digitalWrite(reset, LOW);
delay(1500);
digitalWrite(reset, HIGH);
Serial.begin(9600);
Serial.setTimeout(10);
while(!Serial);
}
void loop() {
if (Serial.available()) {
int val = Serial.parseInt();
if (val > 255) {
Serial.println("ERR: Value out of range");
} else {
shiftOut(data, clock, LSBFIRST, val);
}
}
}
Could someone tell me please what I am doing wrong?
To zmo:
5
5
5
5
5
>ENTERED A NUMBER<
0 ; seperate_mod = 0
1
4 ; END of conditional
5
5
5
5
5
5
5
5
5
5
5
5
5
5
>ENTERED A NUMBER<
0 ; seperate_mod = 1
2
4 ; END of conditional
5
5
5
5
5
5
>ENTERED A NUMBER<
0 ; seperate_mod = 2
3
4 ; END of conditional
5
5
5
5
5
5
5
Could someone tell me please what I am doing wrong?
your code looks mostly fine at first glance, so it's likely to be something else in the context of your code that's not going right.
Have you tried adding debugging printout statements?
Have you tried using a debugger (using a device such as the Atmel ICE)?
It's a sane thing to doubt yourself when what you wrote is not working the way you expect — assuming PEBCAK is often the first best answer. But then, running through your code, displaying values along the way will prove to you whether you're doing it right or wrong, and if it's wrong where is that.
Then, it feels to me like your code isn't actually running at all, either because Serial initialisation is blocking forever in setup() or because Serial.available() is never being true for some other reasons.
Can you try adding that other debug print statement:
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()) {
int seperate_mod = seperate % 3;
Serial.print("0 ; seperate_mod = ");
Serial.println(seperate_mod);
if (seperate_mod == 0) {
Serial.println("1");
val = Serial.parseInt();
shiftOut(data_r, clock_r, MSBFIRST, val);
} else if (seperate_mod == 1) {
Serial.println("2");
val = Serial.parseInt();
shiftOut(data_g, clock_g, MSBFIRST, val);
} else if (seperate_mod == 2) {
Serial.println("3");
val = Serial.parseInt();
shiftOut(data_b, clock_b, MSBFIRST, val);
}
seperate += 1;
Serial.println("4 ; END of conditional");
}
Serial.println("5");
}
and update your question with the resulting output?

Arduino sensor data

Hi I am using an Arduino Flex sensor to control a video game character.
The sensor data is being averaged and remapped to a value of between 0-6.
When the player flexes their bicep it reads the max value (this is perfect) however if the player flexes their arm hard and the sensor reads a max value of 6 for some reason as the player relaxes their arm the declining flex values are being passed into the game engine (instead of going from 6 to zero it drops from 6 to 5 to 4 to 3 to 2 to 1 before reaching zero). Can someone please advise how I should alter my code to make the sensor reading return to 0 instead of declining gradually?
#define NUM_LED 6 //sets the maximum numbers of LEDs
#define MAX_Low 75 //for people with low EMG activity
#define MAX_High 150//for people with high EMG activity
#define Threshold 3 // this sets the light to activate TENS
int reading[10];
int finalReading;
int MAX = 0;
int TENS =3;
int ledState = LOW;
byte litLeds = 0;
byte multiplier = 1;
byte leds[] = {8, 9, 10, 11, 12, 13};
char ch;
char contact;
void setup(){
Serial.begin(9600); //begin serial communications
digitalWrite(TENS, LOW);
for(int i = 0; i < NUM_LED; i++){ //initialize LEDs as outputs
pinMode(leds[i], OUTPUT);
pinMode(TENS, OUTPUT); // Set TENS output to StimPin
}
MAX = MAX_High; //This sets the default to people with high EMG activity.
}
void loop(){
for(int i = 0; i < 10; i++){ //take ten readings in ~0.02 seconds
reading[i] = analogRead(A0) * multiplier;
delay(2);
}
for(int i = 0; i < 10; i++){ //average the ten readings
finalReading += reading[i];
}
finalReading /= 10;
for(int j = 0; j < NUM_LED; j++)
{
digitalWrite(leds[j], LOW);//write all LEDs low and stim pin low
}
finalReading = constrain(finalReading, 0, MAX);
litLeds = map(finalReading, 0, MAX, 0, NUM_LED);
Serial.println(litLeds);
for(int k = 0; k < litLeds; k++){
digitalWrite(leds[k], HIGH); // This turns on the LEDS
}
{
// send data only when you receive data:
if (Serial.available() > 0)
{
ch = Serial.read();
contact=digitalRead(TENS);
if (ch == 'A' && contact==LOW)
{
digitalWrite(TENS, HIGH);
}
else if (ch == 'B' && contact==HIGH)
{
digitalWrite(TENS, LOW);
}
}
}
delay(80);
}
Your sensor doesn't read 6 and immediately 0, you are reading it so fast that you can read the intermediate values. For example: if player opens the arm very fast (250-300 ms, it is fast), you can take up to 3 readings then you can read 3 intermediate values.
You can add more time to the last delay() or use only a value that is the same that the 3 last readings to ensure that there isn't a fast change.

Detect change in Arduino potentiometer

I am trying to write my arduino code to upon detection of a change in the potentiometer analog read value to execute a function.
My question is how do I detect a change in the potentiometer value, I am reading in the potentiometer as normally done, but I am stuck as to how to compare this to see if it has changed.
My loop code for reading potentiometer value:
void loop()
{
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
Serial.println(val);
delay(15);
if (val >= 90)
{
sendSMS5();
delay(10000);
switchOff();
}
}
I am thinking that maybe a number of IF statments to compare if the value falls into a certain bracket is the only way to do this.
Save the value in a variable declared outside the loop.
#define TOLERANCE 10
int oldVal = 0;
void loop()
{
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
Serial.println(val);
delay(15);
int diff = abs(val - oldVal);
if(diff > TOLERANCE)
{
oldVal = val; // only save if the val has changed enough to avoid slowly drifting
// and so on
}
if (val >= 90)
{
sendSMS5();
delay(10000);
switchOff();
}
}

Resources